【问题标题】:Event delegation after removing class移除类后的事件委托
【发布时间】:2017-06-13 15:34:58
【问题描述】:

我有一个按钮

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

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

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

我怎样才能让它工作

【问题讨论】:

标签: javascript jquery events event-delegation


【解决方案1】:

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>

【讨论】:

    猜你喜欢
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多