使用jQuery中的bind()和each()分配事件处理程序的区别? [英] The difference between assigning event handlers with bind() and each() in jQuery?

查看:266
本文介绍了使用jQuery中的bind()和each()分配事件处理程序的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我使用bind()分配事件处理程序的区别:

  $(function(){
$('someElement')
.bind('mouseover',function(e){
$(this).css({
// change color
} );
})
.bind('mouseout',函数(e){
$(this).css({
//返回前一状态

});
})
.bind('click',function(e){
$(this).css({
// do smth。
});
})

});

并将each()用于相同任务:

  $('someElement')。each(function(){

$(this).mouseover(function(){$(this)。 css({/ * change color * /})
.mouseout(function(){$(this).css({/ * return to previous state * /});
});
});
});

谢谢。

解决方案

从你给出的例子中,我想你实际上是在问使用'bind'方法和'event'方法之间有什么区别,如果有的话。

例如,有什么不同:

  $('。some_element')。bind 'click',function(){/ * do stuff * /}); 

...和这个?



<$ p $(p。code> $('。some_element')。click(function(){/ * do stuff * /});

答案是,这并不重要。这是一个偏好问题。事件方法在语法上更简单,涉及的输入较少,但据我所知,确实没有任何区别。我更喜欢使用绑定方法,因为如果您需要将多个事件附加到相同的操作,则可以使用速记事件绑定。它还使得理解何时/是否需要解除绑定事件变得更加简单。



请参阅: .bind和其他事件之间的区别



但是,从什么真正的问题是,'每个'方法和'绑定'方法之间有什么区别......好吧,这是一个完全不同的野兽。



真的使用'each'方法来附加事件,因为'bind'和'event'方法使用更快的CSS选择器引擎(在jQuery中,它使用Sizzle引擎)。



几乎没有(或从未)出现这种情况:

  $(' .some_element')。each(function(){$(this).click(function(){/ * do something * /});}); 

...比这更好:

  $('。some_element')。bind('click',function(){/ * do stuff * /}); 


can someone tell me what the difference between assigning event handlers using bind():

$(function(){
           $('someElement')
           .bind('mouseover',function(e) {
            $(this).css({
                        //change color
                        });
    })
    .bind('mouseout',function(e) {
        $(this).css({
                    //return to previous state

                 });    
    })
    .bind('click',function(e) {
        $(this).css({
                    //do smth.
                 });    
    })

}); 

and using each() for the same task:

$('someElement').each(function(){

        $(this).mouseover(function(){$(this).css({/*change color*/})
                    .mouseout(function(){$(this).css({/*return to previous state*/});   
                    });     
                }); 
    });

thank you.

解决方案

From the examples you gave, I think you're actually asking what the difference, if any, is there between using the 'bind' method and then 'event' methods.

For example, what's the difference between:

$('.some_element').bind('click',function() { /* do stuff */ });

... and this?

$('.some_element').click(function() { /* do stuff */ });

The answer is that it really doesn't matter. It's a matter of preference. The event methods are syntactically simpler and involve less typing, but, as far as I know there really isn't any difference. I prefer to use the bind methods because you can use shorthand event binding if you need to attach more than one event to the same action. It also makes it simpler to understand when/if you need to 'unbind' an event.

See here: Difference between .bind and other events

But, from what the actual question asks, "What's the difference between the 'each' method and the 'bind' method"... well, that's a totally different beast.

You should never really use the 'each' method to attach events because the 'bind' and 'event' methods use the much quicker CSS selector engine (in jQuery's case, it uses the Sizzle engine).

There's hardly ever (or never) a case where this:

$('.some_element').each(function() { $(this).click(function() { /* do something */ }); });

... is better than this:

$('.some_element').bind('click',function() { /* do stuff */ });

这篇关于使用jQuery中的bind()和each()分配事件处理程序的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆