js 中出现这个个问题Cannot read property 'style' of undefined
<divclass="ctrl"><ahref="#"class="ctrl-i"id="ctrl-2"><imgsrc="images/index.jpg"></a><...
<div class="ctrl">
<a href="#" class="ctrl-i" id="ctrl-2">
<img src="images/index.jpg">
</a><a href="#" class="ctrl-i" id="ctrl-2">
<img src="images/index.jpg">
</a><a href="#" class="ctrl-i" id="ctrl-2">
<img src="images/index.jpg"></a>
</div>
window.onload = function() {
my_change("ctrl-i");
}
function my_change(className) {
// 图形变换
var my_obj = document.getElementsByClassName(className);
for (var i = 0; i <= my_obj.length; i++) {
my_obj[i].onmouseover = function () {
// body... 鼠标经过
my_obj[i].style.backgroundColor = "#000";
}
}
} 展开
<a href="#" class="ctrl-i" id="ctrl-2">
<img src="images/index.jpg">
</a><a href="#" class="ctrl-i" id="ctrl-2">
<img src="images/index.jpg">
</a><a href="#" class="ctrl-i" id="ctrl-2">
<img src="images/index.jpg"></a>
</div>
window.onload = function() {
my_change("ctrl-i");
}
function my_change(className) {
// 图形变换
var my_obj = document.getElementsByClassName(className);
for (var i = 0; i <= my_obj.length; i++) {
my_obj[i].onmouseover = function () {
// body... 鼠标经过
my_obj[i].style.backgroundColor = "#000";
}
}
} 展开
1个回答
展开全部
window.onload = function () {
my_change("ctrl-i");
function my_change(className) {
// 图形变换
var my_obj = document.getElementsByClassName(className);
for (var i = 0; i < my_obj.length; i++) {
(function (index) {
my_obj[index].onmouseover = function () {
// body... 鼠标经过
my_obj[i].style.backgroundColor = "#000";
}
})(i)
}
}
}
追问
这个是什么原理来着!!!index是哪里来的参数也没有定义呀!!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询