开发者

jQuery event firing

If I have a code like the following;

if (x == "0")
{
     $("input:checkbox").parent().mouseover(function () {
     //Some code
     }
}

My question is will the code get executed on each mouseover OR it will first check the x == "0" condition and then fire ?

In other words, is the $("input:checkbox").parent().mouseover() code similar to what one would get with a bind() or live() function (which gets fired every time on that event) and the enclosing condition of "x" won't matter ?

Is there any way by which we can link the event with the "x" condition like limiting it's scope only if x is true?

I am not really sure if my question is really valid. But it would be rea开发者_C百科lly great if you could clarify.


My question is will the code get executed on each mouseover OR it will first check the x == "0" condition and then fire ?

It'll get executed on each mouseover, if x == "0" at the point you have written this code.

If you want to only execute a piece of code if x == "0", try

 var x = "0"; // or any other value.
 $("input:checkbox").parent().mouseover(function () {
   if(x == "0") {
     // code goes here
   }
 }


the event will be attached the first time that x has the value = 0. and then it will be executed on each onMousehover, you could detach the event if the x is different to 0.

if (x == "0")
{
    $("input:checkbox").parent().mouseover(function () {
    //Some code
} else {
    $("input:checkbox").parent().unbind('mouseover');
}


Once the mouseover event is attached, it will fire regardless of X. You can get the effect you are looking for by checking X inside the callback function.

 var x = 0;

 $("input:checkbox").parent().mouseover(function () {
  if(x != 0) { return; }
  // some code
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜