删除课后的事件委托

时间:2017-06-13 15:34:59

标签: javascript jquery events event-delegation

我有一个按钮

<button data-button='next' class="disabled">next</button>

我删除已禁用的类以激活但该事件不起作用

   $("[data-button='next']:not(.disabled)").on('click', document, function(){

我怎样才能实现这个目标

1 个答案:

答案 0 :(得分:3)

这不是event delegation的正确语法,请按如下方式更新。

// provide any selector which is parent of the dynamic element
//--\/-- and present when handler is attaching
$(document).on('click', "[data-button='next']:not(.disabled)", function(){
   //  --------------------^^^^^---- provide the selector of corresponding  dynamic element
   // code here
});

$(document).on('click', "[data-button='next']:not(.disabled)", function() {
  console.log('clicked');
});

$("[data-button='next']").removeClass('disabled')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button data-button='next' class="disabled">next</button>