为什么浏览器说'style'这个属性是undefined啊?


<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>myTest</title>

<style>

body{

margin: 0;

padding: 0;

}

ul{ width: 400px;

height: 340px;

list-style: none;

position: relative;

}

li{

width: 100px;

height: 40px;

cursor: pointer;

float: left;

}


li:nth-child(1){

background: blue;

}

li:nth-child(2){

background: green;

}

li:nth-child(3){

background: yellow;

}

li:nth-child(4){

background: red;

}


div{

width: 400px;

height: 300px;

display: none;

position: absolute;

left: 40px;

top: 40px;

}


li:nth-child(1) div{

background: blue;

display: block;

}

li:nth-child(2) div{

background: green;

}

li:nth-child(3) div{

background: yellow;

}

li:nth-child(4) div{

background: red;

}

</style>

<script>

window.onload=function(){


var oLi = document.getElementsByTagName('li');

var oDiv = document.getElementsByTagName('div');


for (var i = 0; i < oLi.length; i++) {

oLi[i].onclick = function(){

oDiv[i].style.display = "block";

}

}


}

</script>

</head>

<body>

<ul>

<li>

<div></div>

</li>

<li>

<div></div>

</li>

<li>

<div></div>

</li>

<li>

<div></div>

</li>

</ul>

</body>

</html>


qq_烟火里的尘埃_0
浏览 1940回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript