<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html" charset="utf-8"><title></title><style>div,p,ul,li{margin:0;padding:0;}li{list-style: none;}a{text-decoration:none;}div{position: relative;}</style><script>window.onload=function(){var b = document.getElementById("s");var str = "";for(var i=0;i<16;i++){str += "<div class='cube' style='width:50px;height: 50px;background: red;position: absolute;left:"+i*70+"px;' ></div>";}document.body.innerHTML=str;var timer = null;var num = 0;var oCubes = getByClass(document,"cube");alert(oCubes[0].style.width);};function getStyle(obj,attr){return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj,null)[attr];};function getByClass(obj,Name){var arr = [];obj= obj?obj:document;var aElement = obj.getElementsByTagName('*');for(var i=0;i<aElement.length;i++){if(aElement[i].className==Name){arr.push(obj);}}return arr;};</script></head><body></body></html>如果上面的getByClass一段換成var oCubes = document.getElementsByTagName("div");alert就可以彈出oCube[0]的寬度,不知道是為什么
為什么使用getByClass取到的數(shù)組里面的對(duì)象取不到屬性?
ITMISS
2022-05-19 15:15:41