var path = document.getElementsByTagName("path");
(function fillCss(){
for(var i in path){
path[i].setAttribute("fill","none");
path[i].setAttribute("stroke","black");
path[i].setAttribute("stroke-width","0.5");
}
})();
样式被执行但是报错:
Uncaught TypeError: path[i].setAttribute is not a function(…)
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
同楼上 quiet_coder 说的
直接 for (var i = 0, l = path.length; i < l; i += 1) ... 就可以了。
for-in 罗列的是对象中PropertyIsEnumerable()的属性,所以path这个类数组中所有PropertyIsEnumerable()的属性都被罗列出了,包含 <length, item, namedItem>这些
因为path是一个类数组,内部包含一个length属性,这个length属性也是参与
for in遍历的,然而他并没有setAttribute,所以此处会报错var path = document.getElementsByTagName("path");