正在将事件处理上的.not()添加到动态添加的内容中



通过对动态添加的内容进行事件处理,我使用:

$('#static-div').on('click', '.dynamic-content', function () {
// do something
});

如果我想向代码中添加.not((,该怎么办?在中,我想用"单击任何内容;。动态内容";但不将事件处理应用于";。不应该被点击";

将:not((添加到选择器

$('#static-div').on('click', '.dynamic-content:not(.no)', function () {
console.log('clicked')
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="static-div">
<button type="button" class="dynamic-content">Y</button>
<button type="button" class="dynamic-content no">N</button>
<button type="button" class="dynamic-content">Y</button>
<button type="button" class="dynamic-content no">N</button>
</div>

您可以添加一个非css选择器。

在这里查看

$('#static-div').on('click', '.dynamic-content:not(.should-not-be-clicked) ', function () {
// do something
});

最新更新