使用 data-custom= " "绑定到事件



我很确定我会在这个问题上受到猛烈抨击。

我喜欢使用 data-any 属性将事件绑定到。

这对我来说感觉很干净,有助于保留我的类属性,仅用于样式。

我知道这个选择器是最慢的,所以当有很多元素时我不使用它。

很想听到令人信服的反对意见。

$("body").delegate("[data-action]", "click", function(){
var action = $(this).attr("data-action");
//route action to appropriate function
});
$("body").delegate("[data-action]", "click", function(){
   ^^^^-------------------------- body is a too high node.
          ^^^^^^^^--------------- on should be used now instead of delegate.
                    ^^^^^^^^^^^^^ attribute selector is a slow selector.