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]的宽度,不知道是为什么
添加回答
举报
0/150
提交
取消