javascript - 删除类后的事件委托(delegate)

标签 javascript jquery events event-delegation

我有一个按钮

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

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

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

我怎样才能让它工作

最佳答案

这不是 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>

关于javascript - 删除类后的事件委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44525888/

相关文章:

javascript - 制作生命条时使用 onClick(游戏)

php - jquery,javascript 用@更改文本以链接

javascript - jquery 提交事件并返回 false

events - 函数式事件驱动编程

email - Outlook iCal session 邀请说明问题

javascript - 是什么导致我的测试中出现 "Uncaught Error: write after end"?

javascript - 单击时落下的气球/图像

jquery cookie : a new cookie is created on each page?

javascript - 使用需要不同 jQuery 版本的控件

javascript - jQuery/JavaScript 冒泡和停止传播不起作用