通过js对层的属性进行赋值的有关问题
通过js对层的属性进行赋值的问题
html代码
<link rel = "stylesheet" href = "./base.css"/>
<script src = "./base.js"></script>
</head>
<body>
<div id = "head">
<div class = "hed"></div>
<div class = "hed"></div>
<div class = "hed"></div>
<div class = "hed"></div>
</div>
</body>
</html
css代码
#head{ border:1px solid red; width:500px; height:300px;
-webkit-transition:all .9s ease;}
.hed{border:1px solid red; width:50px; height:300px; float:left;}
这是js代码
window.onload = function()
{init();}
function init(){
var el = document.getElementById("head");
var els = document.getElementsByClassName("hed");
for(var i =0;i<els.length;i++)
{
els[i].onmousemove = function()
{
els[0].style.backgroundColor = "red"//
}
}
}
如果给onmousemove的事件内的语句是els[0].style.backgroundColor = "red"就能行,改为els[i].style.backgroundColor = "red"
就会出现Uncaught TypeError: Cannot read property 'style' of undefined 为什么
要实现鼠标经过四个子层是每个层的颜色都变化怎么实现,我的这种方法不行;
------解决思路----------------------
els[i].onmousemove = function()
{
this.style.backgroundColor = "red"//
}
------解决思路----------------------
你在事件中输出下 i 的值就明白了
html代码
<link rel = "stylesheet" href = "./base.css"/>
<script src = "./base.js"></script>
</head>
<body>
<div id = "head">
<div class = "hed"></div>
<div class = "hed"></div>
<div class = "hed"></div>
<div class = "hed"></div>
</div>
</body>
</html
css代码
#head{ border:1px solid red; width:500px; height:300px;
-webkit-transition:all .9s ease;}
.hed{border:1px solid red; width:50px; height:300px; float:left;}
这是js代码
window.onload = function()
{init();}
function init(){
var el = document.getElementById("head");
var els = document.getElementsByClassName("hed");
for(var i =0;i<els.length;i++)
{
els[i].onmousemove = function()
{
els[0].style.backgroundColor = "red"//
}
}
}
如果给onmousemove的事件内的语句是els[0].style.backgroundColor = "red"就能行,改为els[i].style.backgroundColor = "red"
就会出现Uncaught TypeError: Cannot read property 'style' of undefined 为什么
要实现鼠标经过四个子层是每个层的颜色都变化怎么实现,我的这种方法不行;
------解决思路----------------------
els[i].onmousemove = function()
{
this.style.backgroundColor = "red"//
}
------解决思路----------------------
你在事件中输出下 i 的值就明白了