javascript – 类的MutationObserver(不是id)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 类的MutationObserver(不是id),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2868字,纯文字阅读大概需要5分钟。
内容图文
使MutationObserver适用于#someID并不是问题,但是它能使它适用于.someClass的方法是什么?
目前我正在使用以下内容:
// this example doensn't work,
// as well as many another attempts
var target = document.querySelectorAll(".someClass");
for (var i = 0; i < target.length; i++) {
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var foo = target[i].getAttribute("someAttribute")
if (foo == "someValue")
foo.style.backgroundColor = "red";
});
});
// configuration of the observer
var config = { attributes: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
}
解决方法:
你有几个问题:
> iterator:执行代码后,target [i]不是您所期望的(var foo = target [i] .getAttribute(“someAttribute”)),因为在运行此行时迭代完成,我的值为target.length,所以target [i]不存在
>属性没有样式(foo.style.backgroundColor),需要引用目标元素
>您将整个集合传递给观察者(observer.observe(target,config);)您只需要一个目标元素
这是修复上面列出的错误并将循环代码外部化为函数以便更容易进行目标引用之后的工作代码:
var target = document.querySelectorAll(".c");
for (var i = 0; i < target.length; i++) {
create(target[i]);
}
function create(t) {
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var foo = t.getAttribute("aaa")
if (foo == "vvv")
t.style.backgroundColor = "red";
});
});
// configuration of the observer
var config = {
attributes: true
};
// pass in the target node, as well as the observer options
observer.observe(t, config);
}
// let's change an attribute in a second
setTimeout(function(){
target[2].setAttribute('aaa', 'vvv');
}, 1000);
.c {
width: 50px;
height: 50px;
display: inline-block;
border: 1px solid black
}
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
UPDATE
这是一个包含最少编辑的示例:
> var foo = target [i] .getAttribute(“someAttribute”)更改为var foo = mutation.target.getAttribute(“someAttribute”)而不是传入的目标元素
var target = document.querySelectorAll(".someClass");
for (var i = 0; i < target.length; i++) {
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var foo = mutation.target.getAttribute("someAttribute")
if (foo == "someValue")
mutation.target.style.backgroundColor = "red";
});
});
// configuration of the observer
var config = { attributes: true };
// pass in the target node, as well as the observer options
observer.observe(target[i], config);
}
// let's change an attribute in a second
setTimeout(function(){
target[2].setAttribute('someAttribute', 'someValue');
}, 1000);
.someClass {
width: 50px;
height: 50px;
display: inline-block;
border: 1px solid black
}
<div class="someClass"></div>
<div class="someClass"></div>
<div class="someClass"></div>
<div class="someClass"></div>
内容总结
以上是互联网集市为您收集整理的javascript – 类的MutationObserver(不是id)全部内容,希望文章能够帮你解决javascript – 类的MutationObserver(不是id)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。