问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/7837510/domsubtreemodified-not-working-in-chrome