TypeError: node.setAttribute is not a function

天涯浪子 提交于 2019-12-05 12:11:29
Tim Down

Chances are the node on which you're calling setAttribute() is a text node rather than an element. The easiest solution is check the nodeType property before calling setAttribute():

var item = colors.childNodes[i];
if (item.nodeType == 1) {
    // Do element stuff here
}

An aside: setting event handler attributes such as onmouseover via setAttribute() is generally a bad idea because it doesn't work as specified in older IE (and compatibility modes in later IE). Use the equivalent property instead:

item.onmouseover = function() {
    moveover(this.src);
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!