DOMSubtreeModified 在 Chrome 中不起作用
我的这段代码在 Firefox 中完美运行。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
function CreateEvent()
{
var x = document.createEvent("MutationEvent");
x.initMutationEvent("DOMSubtreeModified", true, false, null, "", "", "", 0);
document.dispatchEvent(x);
}
$(document).ready(function() {
$('#myDiv')[0].addEventListener('DOMNodeRemoved', function () { console.log('Content removed!'); }, false);
$('#myDiv')[0].addEventListener('DOMSubtreeModified', function () { console.log('DOMSubtreeModified modified!'); }, false);
$('#myDiv')[0].addEventListener('DOMNodeRemovedFromDocument', function () { console.log('DOMNodeRemovedFromDocument !'); }, false);
$('#myDiv')[0].addEventListener('DOMNodeInserted', function () { console.log('DOMNodeInserted !'); }, false);
$('#myBtn').click(function (e) {
$('#myDiv').css({ marginLeft : "300px"});
});
});
</script>
</head>
<body>
<div id="myDiv">Content</div>
<div id="myBtn">
Button
</div>
</body>
</html>
但它不适用于 Chrome。我希望每次 myDiv 发生问题时都会触发一个事件。 (删除,CSS修改,一切)。
有什么解决方法吗?
I have this code working perfectly in firefox.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
function CreateEvent()
{
var x = document.createEvent("MutationEvent");
x.initMutationEvent("DOMSubtreeModified", true, false, null, "", "", "", 0);
document.dispatchEvent(x);
}
$(document).ready(function() {
$('#myDiv')[0].addEventListener('DOMNodeRemoved', function () { console.log('Content removed!'); }, false);
$('#myDiv')[0].addEventListener('DOMSubtreeModified', function () { console.log('DOMSubtreeModified modified!'); }, false);
$('#myDiv')[0].addEventListener('DOMNodeRemovedFromDocument', function () { console.log('DOMNodeRemovedFromDocument !'); }, false);
$('#myDiv')[0].addEventListener('DOMNodeInserted', function () { console.log('DOMNodeInserted !'); }, false);
$('#myBtn').click(function (e) {
$('#myDiv').css({ marginLeft : "300px"});
});
});
</script>
</head>
<body>
<div id="myDiv">Content</div>
<div id="myBtn">
Button
</div>
</body>
</html>
But it's not working on Chrome. I want an event triggered everytime something happens to myDiv. (remove, css modifcation, everything).
Any work-arounds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以后就不行了DOM 突变事件已被弃用,并且今后将不再受支持,http://www.w3.org/TR/DOM-Level-3-Events/#events-mutationevents。
It won't work going forward. DOM mutation events have been deprecated and won't be supported going forward, http://www.w3.org/TR/DOM-Level-3-Events/#events-mutationevents.