您可以使用 aMutationObserver
来实现这一点。要跟踪从contenteditable
元素中删除的任何类型的节点,请观察以下示例
<div id="myTextArea" contenteditable="true">
<input value="input" />
<span contenteditable="false">span</span>
</div>
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
//console.log($(mutation.removedNodes)); // <<-- includes text nodes as well
$(mutation.removedNodes).each(function(value, index) {
if(this.nodeType === 1) {
console.log(this) // your removed html node
}
});
});
});
var config = { attributes: true, childList: true, characterData: true };
observer.observe($('#myTextArea')[0], config);