问题
Why doesn't this code work in IE? Please help fix it:
jQuery('body').live('DOMNodeInserted',function(e){
var parent = jQuery(e.target).parent();
parent.find("a").css('color','#AA62C6');
parent.find('a').removeAttr('onmousedown');
});
回答1:
This event is not supported in IE. This is added to IE9 but seems to be buggy in the implementation.
A solution will be to handle the dom manipulation at the base(The method which is changing the dom) level.
function update(){
//do some dom manipulation
$(window).trigger('customupdatedom', parent);
}
$(window).on('customupdatedom', function(e, parent){
//handle dom change
})
You can also read the following
DOMNodeInserted equivalent in IE?
DOMNodeInserted event
回答2:
Use onreadystatechange for IE:
var parent;
if (!!document.addEventListener)
{
jQuery('body').live('DOMNodeInserted',function(e){
parent = jQuery(e.target).parent();
parent.find("a").css('color','#AA62C6');
parent.find('a').removeAttr('onmousedown');
});
}
else
{
jQuery("body").get(0).addBehavior("foo.htc");
jQuery('body').get(0).attachEvent('onreadystatechange',function(e){
parent = jQuery(e.target).parent();
parent.find("a").css('color','#AA62C6');
parent.find('a').removeAttr('onmousedown');
});
}
来源:https://stackoverflow.com/questions/15018590/domnodeinserted-in-the-ie