gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:35:40 24 4
gpt4 key购买 nike

我有一个按钮

<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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com