我正在进行一些小扩展,以增强一个网站,即谷歌加邮箱.我想把它移到显示器的最左边.
但是,此帖子每次打开时都会重置其值.
基本上我想要消除这种行为,并认为我可以只监视element.style更改的元素,并再次覆盖它们.然而,DOMAttrModified似乎不适用于那样的东西
除此之外,我发现当邮箱关闭时它不再存在了吗?
也许这里有人知道如何解决这个问题我当然可以循环一个每隔一秒左右设置一次风格的操作.但不,谢谢XD
非常感谢帮助:)
不推荐使用Mutation事件,webkit浏览器不支持DOMAttrModified,也不支持DOMAttrModified .请改用Mutation Observers.或者,您可以尝试此解决方法.
为了让您快速开始使用Mutation Observer,
这里有一个小的可重用函数
/**
* Mutation Observer Helper function
* //developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe
* @param {string} sel The DOM selector to watch
* @param {object} opt MutationObserver options
* @param {function} cb Pass Mutation object to a callback function
*/
const Observe = (sel, opt, cb) => {
const Obs = new MutationObserver((m) => [...m].forEach(cb));
document.querySelectorAll(sel).forEach(el => Obs.observe(el, opt));
};
Run Code Online (Sandbox Code Playgroud)
要仅跟踪"style"某些class="item"元素的属性更改,请使用类似:
Observe(".item", {
attributesList: ["style"], // Only the "style" attribute
attributeOldValue: true, // Report also the oldValue
}, (m) => {
console.log(m); // Mutation object
});
Run Code Online (Sandbox Code Playgroud)
要监视所有属性的更改,而不是attributesList使用数组:
attributes: true
Run Code Online (Sandbox Code Playgroud)
如果需要的话,这里有一个例子:
attributes: true
Run Code Online (Sandbox Code Playgroud)
/**
* Mutation Observer Helper function
* //developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe
* @param {string} sel The DOM selector to watch
* @param {object} opt MutationObserver options
* @param {function} cb Pass Mutation object to a callback function
*/
const Observe = (sel, opt, cb) => {
const Obs = new MutationObserver((m) => [...m].forEach(cb));
document.querySelectorAll(sel).forEach(el => Obs.observe(el, opt));
};
// DEMO TIME:
Observe("#test", {
attributesList: ["style"],
attributeOldValue: true,
}, (m) => {
console.log(`
Old value: ${m.oldValue}
New value: ${m.target.getAttribute(m.attributeName)}
`);
});
const EL_test = document.querySelector("#test");
EL_test.addEventListener("input", () => EL_test.style.cssText = EL_test.value);
EL_test.style.cssText = EL_test.value;Run Code Online (Sandbox Code Playgroud)
* {margin:0;}
textarea {width: 60%;height: 50px;}Run Code Online (Sandbox Code Playgroud)