【问题标题】:Difference between .bind and other events.bind 和其他事件的区别
【发布时间】:2010-10-08 10:23:23
【问题描述】:

以下代码行之间有什么区别,或者它们只是编写相同内容的两种不同方式:

$("p").click(function() { some code here });

$("p").bind("click", function(){ some other code here });

我是否过度简化了这个?因为如果您想绑定多个事件,您可以将事件链接起来,对吗?

【问题讨论】:

  • 我的问题措辞更好:) 但是是的,我现在看到了骗局
  • 这个问题更笼统也更容易找到,所以我不认为它是重复的。

标签: jquery


【解决方案1】:

它还允许您将相同的匿名方法绑定到多个事件,例如:

$("p").bind("click dblclick mouseover mouseout", function(){ some other code here });

【讨论】:

    【解决方案2】:

    另请注意,绑定允许自定义事件

    $(elem).bind('myEvent', function(){
       alert('myEvent!');
    });
    $(elem).trigger('myEvent'); //alerts 'myEvent!'
    

    【讨论】:

      【解决方案3】:

      第一个版本只是第二个版本的简写。

      【讨论】:

        猜你喜欢
        • 2023-03-04
        • 2020-08-10
        • 1970-01-01
        • 1970-01-01
        • 2012-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-26
        相关资源
        最近更新 更多