js的问题为什么用innerHTML改变网页结构以后getStyle取得的属性是空值

<!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;}

body{}

.redCube{width:50px;height: 50px;background: red;position: absolute;}

#s{width: 100px;height: 100px;background: blue;}

</style>

<script>

window.onload=function(){


var b = document.getElementById("s");

alert(getStyle(b,'width'));

document.body.innerHTML+="<div class='redCube' ></div>";

alert(getStyle(b,'width')); 

};


function getStyle(obj,attr){

return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj,null)[attr];

};


</script>

</head>

<body>

<div id="s"></div> 

</body>


</html>


EverDream3726731
浏览 920回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript