在jQuery中使用bind()和each()分配事件处理程序之间的区别?

时间:2022-08-26 10:31:31

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

谁能告诉我使用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:

并使用each()执行相同的任务:

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

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

thank you.

1 个解决方案

#1


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.

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

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

请参阅此处:.bind与其他事件之间的区别

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).

你永远不应该真的使用'each'方法来附加事件,因为'bind'和'event'方法使用更快的CSS选择器引擎(在jQuery的情况下,它使用Sizzle引擎)。

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 */ });

#1


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.

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

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

请参阅此处:.bind与其他事件之间的区别

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).

你永远不应该真的使用'each'方法来附加事件,因为'bind'和'event'方法使用更快的CSS选择器引擎(在jQuery的情况下,它使用Sizzle引擎)。

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 */ });