js,用getByClass取到的数组里面的对象取不到属性,getElementsByTagName就可以,为什么呢? 150
<!DOCTYPEhtml><html><head><metahttp-equiv="Content-Type"content="text/html"charset="u...
<!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]的宽度,不知道是为什么 展开
<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]的宽度,不知道是为什么 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询