老师好!这里面的this为什么,不能换成lis[i];
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
html,
body {
margin: 0;
padding: 0;
}
div:not(:nth-of-type(2)) {
width: 500px;
height: 100%;
background-color: #a0e4ff;
margin: 0 auto;
}
/*ul默认有外边距*/
ul {
width: 300px;
height: 100%;
background-color: #ecc7ea;
list-style: none;
/*清除默认边距*/
margin: 0;
padding: 0;
margin: 20px auto;
margin-bottom: 30px;
}
li {
width: 200px;
height: 30px;
line-height: 30px;
margin: 30px auto;
background-color: #cdffc0;
}
</style>
</head>
<body>
<div id="box">
<button id="btnAdd">添加元素</button>
<button id="btnRemove">删除元素</button>
<ul id="list">我是ul
<li>我是li1</li>
<li>我是li2</li>
<li>我是li3</li>
</ul>
</div>
<script type="text/javascript">
//此处填写代码
var Asd=document.getElementById("btnAdd"),
rem=document.getElementById("btnRemove"),
ul=document.getElementById("list"),
lis=ul.getElementsByTagName("li");
for(var i=0;i<lis.length;i++){
lis[i].onmouseover=function(){
this.style.backgroundColor="#00f";//问题在此处老师!!!!!
}
lis[i].onmouseout=function(){
this.style.backgroundColor="#cdffc0";//问题在此处老师!!!!!换了浏览器就报错
}
}
Asd.onclick=function(){
var li=document.createElement("li"),
text=document.createTextNode("我是li"+(lis.length+1));
li.appendChild(text);
li.onmouseover=function(){
this.style.backgroundColor="#00f";
}
li.onmouseout=function(){
this.style.backgroundColor="#cdffc0";
}
var fam=document.createDocumentFragment();
fam.appendChild(li);
ul.appendChild(fam);
}
rem.onclick=function(){
if(ul.children.length>0){
ul.removeChild(ul.children[ul.children.length-1]);
}
}
</script>
</body>
</html>
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星