我有一个按钮
<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/