【问题标题】:Jquery events only work for 'html'Jquery 事件仅适用于“html”
【发布时间】:2014-11-19 19:49:29
【问题描述】:
jQuery(window).scroll(function(){
if (jQuery(this).scrollTop() > 100) {
    jQuery('#topcontrol').css({bottom:"15px"});
} else {
    jQuery('#topcontrol').css({bottom:"-100px"});
}

});

jQuery('#topcontrol').click(function(){
jQuery('html, body').animate({scrollTop: '0px'}, 800);
console.log('works');

});

什么都没有发生。控制台中没有显示错误。无论是什么事件——单击、悬停等——它们只有在我将它们应用于“html”标签时才有效。代码的第一部分完美运行,我的意思是 .css。

这是什么魔法?

【问题讨论】:

    标签: javascript jquery events javascript-events event-handling


    【解决方案1】:

    咕噜……我想通了。整个代码需要包含在其中:

    $( document ).ready(function() {
        console.log( "ready!" );
    });
    

    谁能解释一下?有些图片没有找到,这会影响这个吗?

    【讨论】:

    • Javascript 将在加载后立即运行,但页面的其余部分可能尚未加载。您发布的那个 sn-p 将等到文档完全加载后再执行其中包含的 javascript。
    猜你喜欢
    • 2013-06-24
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2013-04-30
    相关资源
    最近更新 更多