粘贴文本后,我想获取目标div的ID。后来div动态生成,代码不起作用:
HTML
<div class="wrap">
<div class="entry" id="1" contenteditable="true">paste here</div>
<div class="entry" id="2" contenteditable="true">paste here</div>
<div class="entry" id="3" contenteditable="true">paste here</div>
<div class="entry" id="4" contenteditable="true">paste here</div>
</div>
JS
$('.wrap').on("paste", function(){
console.log("current box ID: " + $(this).attr("id") );
});
最佳答案
尝试使用父类.wrap
当您粘贴到该div
时,需要使用.entry
类
$('.wrap div.entry').on("paste", function(){
console.log("current box ID: " + $(this).attr("id") );
});
如果 .entry
是动态创建的,那么您可以使用 event delegation
$(document.body).on('paste','.wrap .entry',function(){
console.log("current box ID: " + $(this).attr("id") );
});
关于javascript - jQuery - 粘贴事件后获取元素的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23066446/