我之前有一个问题here,这使我进入了这个阶段,问另一个问题:
我要实现的目标的概述:
我正在尝试使用MutationObserver监视我的文档,然后更改动态加载的输入元素的“类型”。
代码不完整,需要帮助:
var observer = new MutationObserver(function (mutations)
{
mutations.forEach(function (mutation)
{
console.log(mutation.type);
// here I need to get all elements with class "GTnumeric" and
// change its "type" to be "tel" if its not "hidden"
// **please help me out here**
// TRIED CODE
});
});
var config = {
childList: true,
subtree: true
};
// Node, config
var targetNode = document.body;
// or would you rather suggest me to try
var targetNode = document.getElementsByClassName(".GTnumeric");
// setting the observer
observer.observe(targetNode, config);
我不确定如何遍历for循环中返回的突变。我在下面的代码段中提到的CODE位置尝试了以下操作,但得到了: [未捕获的TypeError:t [i] .getAttribute不是函数]
尝试代码
var elems = document.getElementsByClassName(".GTnumeric");
for (var el in elems) {
if (elems[el].getAttribute("type") != 'hidden') {
elems[el].setAttribute("type", "tel");
}
}
由于我不确定如何有效地使用MotationObserver,请进一步指导我。感谢您的帮助!