简体   繁体   English

为什么document.ready中的事件处理函数起作用,但取出后却不起作用?

[英]Why eventhandler function in document.ready works but doesn't work when taken out?

I am new to Javascript and I am wondering on the behavior below. 我是Java语言的新手,我想知道下面的行为。

Consider the working code below: 考虑下面的工作代码

 <div><br/><strong>Click Me!</strong></div>   
 <script>

    $(document).ready(function(){
        $('div').mouseenter(function(){
           $(this).fadeTo('fast',1);
        });

        $('div').mouseleave(function(){
           $(this).fadeTo('fast',0.5); 
        });

    });

 </script>

But this one does not work : 但这不起作用

 <div onmouseover="fade()"><br/><strong>Click Me!</strong></div>   
 <script>

    $(document).ready(function(){

        $('div').mouseleave(function(){
           $(this).fadeTo('fast',0.5); 
        });

    });

    function fade(){
        $(this).fadeTo('fast',1);
    }
 </script>

Why is the second one not working when all I did was use inline eventhandler and function? 当我全部使用内联事件处理程序和函数时,为什么第二个不起作用?

Thanks in advance! 提前致谢!

First, I wouldn't do this. 首先,我不会这样做。 Switching from using modern event handling to onXyz attributes is a bit backward. 从使用现代事件处理切换到onXyz属性有点落后。 See below the fold for more. 请参阅下面的折叠。

But answering the question of why it didn't work: this within the call to fade in your second example is not the div (it's the global object, aka window on browsers). 但是回答为什么它不起作用的问题:在第二个示例中, fade调用中的this不是div(它是全局对象,也就是浏览器上的window )。 You'd have to change your onmouseover to: 您必须将onmouseover更改为:

onmouseover="fade.call(this)"

...for this to be the div during the call. ......对于this是在通话过程中的股利。

(Separately, note that you used onmouseover in the second code block but mouseenter in your first code block. I've left it as onmouseover , but you probably wanted onmouseneter instead.) (另外,请注意您所使用onmouseover在第二个代码块,但mouseenter在你的第一个代码块。我已经离开它onmouseover ,但你可能想onmouseneter代替。)

Example: 例:

 $(document).ready(function(){ $('div').mouseleave(function(){ $(this).fadeTo('fast',0.5); }); }); function fade(){ $(this).fadeTo('fast',1); } 
 <div onmouseover="fade.call(this)"><br/><strong>Click Me!</strong></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

Or alternately, just pass this in as an argument and change fade to use it: 或者,只需将this作为参数传递并更改fade以使用它:

 $(document).ready(function(){ $('div').mouseleave(function(){ $(this).fadeTo('fast',0.5); }); }); function fade(element){ $(element).fadeTo('fast',1); } 
 <div onmouseover="fade(this)"><br/><strong>Click Me!</strong></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 


But again, I wouldn't use an onXyz attribute; 但是同样,我不会使用onXyz属性。 if you don't want the handlers in a ready callback, they don't need to be, but that doesn't mean you have to switch to using attributes for event hookup. 如果您不希望处理程序出现在ready回调中,则不需要它们,但这并不意味着您必须切换到使用属性进行事件连接。 Instead: 代替:

$('div').mouseleave(function(){
  $(this).fadeTo('fast',0.5); 
});
$('div').mouseenter(function(){
  $(this).fadeTo('fast',1); 
});

Example: 例:

 $('div').mouseleave(function(){ $(this).fadeTo('fast',0.5); }); $('div').mouseenter(function(){ $(this).fadeTo('fast',1); }); 
 <div><br/><strong>Click Me!</strong></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

You use document.ready because you don't know if the nodes you're trying to effect have been rendered yet. 之所以使用document.ready是因为您不知道您尝试影响的节点是否已经渲染。

Scripts are executed in-order on the page. 脚本在页面上按顺序执行。 So if you have more div s below your script definition they're not going to match in $("div") . 因此,如果在脚本定义下还有更多div它们将不会在$("div")匹配。

Since scripts are typically included in the header, you totally need document.ready if you want JavaScript to see all the nodes initially. 由于脚本通常包含在标头中,因此如果您希望JavaScript最初查看所有节点,则完全需要document.ready

In your example you don't need document.ready per se. 在您的示例中,您本身不需要document.ready The error is elsewhere. 该错误在其他地方。


<div>1</div>
<script>
  console.log($("div").length); // 1
  $(document).ready(function () {
    console.log($("div").length); // 2
  });
</script>
<div>2</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM