使用Javascript加载页面
问题描述:
我希望页面加载时显示标题和副标题。我尝试了几次让它工作,但它不会。使用Javascript加载页面
jQuery(document).ready(function(){
jQuery('.element').bind('mouseover', function() {
jQuery(this).find('img').stop().each(function() {
jQuery(this).animate({
'opacity': 1
}, 200);
});
});
jQuery('.element').bind('mouseout', function() {
jQuery(this).find('img').stop().each(function() {
jQuery(this).animate({
'opacity': 0.4
}, 200);
});
});
jQuery('.element').on('pageload', function() {
jQuery(this).find('.title').stop().each(function() {
jQuery(this).animate({
'margin-left': 35,
'opacity': 1
}, 250);
});
jQuery(thi2s).find('.subtitle').stop().each(function() {
jQuery(this).animate({
'opacity': 1
}, 0);
jQuery(this).delay(150).animate({
'margin-left': 35
}, 250);
});
});
});
有人可以帮我解决这个问题吗?
答
这不是完全清楚你正在尝试做的,但在第一眼,我就注意到了这个错误:
jQuery(thi2s).find('.subtitle').stop().each(function() {
// code
}
显然,外来的“2”不应该在那里,但我不确定这个小修补程序是否会创建你正在寻找的预期行为。
下面是如何构建代码的示例。您不应该使用.bind()
函数来为您的事件或参考元素使用jQuery('.element')
。此外,过度使用this
是不必要的,并且会使代码复杂化。
考虑这个简单的例子:
$(document).ready(function() {
$("div").hover(
function() {
$("h2").animate({
left: 50,
opacity: 1
}, 500);
}, function() {
$("h2").animate({
left: 50,
opacity: 0.2
}, 500);
});
});
希望这有助于!
pageload?你使用jQuery手机吗? – epascarello 2014-12-27 20:30:35
运行'pageload'的jQuery的版本是否在1.4中被弃用。 – Barmar 2014-12-27 20:31:15
我不知道。我在哪里可以检查? – 2014-12-27 20:32:15