通過對動態添加的內容進行事件處理,我使用:
$('#static-div').on('click', '.dynamic-content', function () {
// do something
});
如果我想在此代碼中添加 .not() 該怎么辦?如,我想點擊任何帶有“.dynamic-content”的東西,但不將事件處理應用于“.should-not-be-clicked”
uj5u.com熱心網友回復:
將 :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>
uj5u.com熱心網友回復:
有一個非 CSS 選擇器,您可以將其添加到其中。
在這里查看
$('#static-div').on('click', '.dynamic-content:not(.should-not-be-clicked) ', function () {
// do something
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/439733.html
標籤:javascript jQuery
