Luk*_*asz 5 html javascript events
我有一个内容可编辑的 div,其中包含一些 html 元素。例如,假设我有:
<div contenteditable="true" id="myTextArea">
Some content, <div id="div1">content in div</div>,
and more <span id="span1">content</span></div>
Run Code Online (Sandbox Code Playgroud)
当用户在 myTextArea 中编辑文本并删除一些 html 元素(通过粘贴新内容或只是退格文本)时,我希望每个 html 元素触发一个事件并告诉我它已被删除。关于我如何做到这一点的任何想法?
您可以使用 aMutationObserver
来实现此目的。要跟踪从元素中删除的任何类型的节点contenteditable
,请观察以下示例
<div id="myTextArea" contenteditable="true">
<input value="input" />
<span contenteditable="false">span</span>
</div>
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1189 次 |
最近记录: |