简体   繁体   English

使用纯JavaScript添加,删除属性或更改其值时如何获取事件

[英]How to get event when an attribute is added, removed or its value is changed with pure javascript

I saw similar questions asked few years back but they are not useful to me. 我看到几年前问过类似的问题,但对我没有用。 I'm repeating somewhat similar question here as there could be new updates. 我在这里重复了类似的问题,因为可能会有新的更新。

I want invoke a function when an attribute is added, removed in an element (or its value changed) for all elements in the document. 我想为文档中的所有元素添加,删除元素(或更改其值)时添加属性。 It needs to be working in all browsers at least chrome and mozilla firefox. 它需要在所有浏览器中都可以使用,至少是chrome和mozilla firefox。 I want to achieve it purely in javascript. 我想纯粹用javascript实现它。

I tried the following codes 我尝试了以下代码

Using event listener. 使用事件监听器。 This works in mozilla firefox but not working in chrome. 这适用于mozilla firefox,但不适用于chrome。

document.addEventListener("DOMAttrModified", function(event){
console.log('DOMAttrModified invoked');
console.log(event);
});

Using observer. 使用观察者。 It doesn't work and it makes error ( WebKitMutationObserver is not defined ) in error firefox. 它不起作用,并在错误Firefox中产生错误( WebKitMutationObserver is not defined )。 In chrome it doesn't make any error but it is not listening to event. 在chrome中,它不会发生任何错误,但它并未监听事件。

var element = document.body, bubbles = false;
var observer = new WebKitMutationObserver(function (mutations) {
  mutations.forEach(attrModified);
});
observer.observe(element, { attributes: true, subtree: bubbles });

Finally, I tried the following. 最后,我尝试了以下方法。

Element.prototype.setAttribute = function(name, value) { 
    console.log('attribute modified');
    console.log(this);
    console.log(name);
    console.log(value);
};

Obviously, it worked in all browsers but only when setting the attribute value with setAttribute . 显然,它适用于所有浏览器,但仅在使用setAttribute设置属性值时有效。 Eg: var div = document.createElement('div'); 例如: var div = document.createElement('div'); but not with div.style = 'color:green'; 但不是div.style = 'color:green'; . I also want to get event when setting value like div.style = 'color:green'; 我也想在设置div.style = 'color:green';时获取事件div.style = 'color:green'; / div.name = 'somename'; / div.name = 'somename'; . Is there any way I can achieve this? 有什么办法可以实现?

WebKitMutationObserver was a temporary "namespaced" thing back before mutation observers were well defined and supported. WebKitMutationObserver是一个临时的“命名空间”,早在突变观察者得到良好定义和支持之前。 Now, you just use MutationObserver , which is well supported : 现在,您只需要使用MutationObserver ,它得到了很好的支持

 var element = document.body, bubbles = false; var observer = new MutationObserver(function (mutations) { console.log(mutations); }); observer.observe(element, { attributes: true, subtree: bubbles }); document.body.style.color = "green"; 

The above works on Firefox, Chrome, IE11, and Edge. 以上适用于Firefox,Chrome,IE11和Edge。

If for some reason you need to support IE9 or IE10, they had some support for the old "mutation events," and there are shims that use mutation events to provide some of the features of mutation observers for obsolete browsers. 如果出于某种原因需要支持IE9或IE10,则它们对旧的“突变事件”也有一定的支持,并且有些垫片使用突变事件为过时的浏览器提供了突变观察器的某些功能。


I also want to get event when setting value like div.style = 'color:green'; 我也想在设置div.style ='color:green'等值时获取事件;

That's not a valid way to set the style attribute, and will not work reliably cross-browser. 这不是设置样式属性的有效方法,并且无法跨浏览器可靠地工作。 Either div.style.color = "green"; div.style.color = "green"; (which will leave other aspects of style alone), or div.setAttribute("style", "color: green"); (将不考虑style其他方面),或div.setAttribute("style", "color: green"); (which will wipe out any other inline styles on it), or on at least some browsers, div.style.cssText = "color:green"; (这将清除其上的任何其他内联样式),或者至少在某些浏览器上, div.style.cssText = "color:green"; (which will also wipe out other inline styles on it). (这也会清除其上的其他内联样式)。

I think Object.observe() may be useful in your case : http://www.html5rocks.com/en/tutorials/es7/observe/ 我认为Object.observe()在您的情况下可能会有用: http ://www.html5rocks.com/en/tutorials/es7/observe/

and implemented similar to the above solution, but Object.observe() not deprecated and part of new webstandards 并实现了与上述解决方案类似的功能,但是Object.observe()未弃用,并且是新的Webstandards的一部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM