是否正确呢
来源:11-2 编程练习
张凤梅
2020-03-03 19:39:02
<!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 btnAdd=document.getElementById("btnAdd");
var btnRemove=document.getElementById("btnRemove");
var ul=document.getElementById("list");
var lis=document.getElementsByTagName("li");
var i=3;
for (var i = 0; i <ul.children.length; i++) {
ul.children.item(i).onmouseover = function () {
this.style.background = 'blue';
}
ul.children.item(i).onmouseout = function () {
this.style.background = 'red';
}
}
btnAdd.onclick=function() {
var newLi=document.createElement("li");
newLi.appendChild(document.createTextNode("我是li"+(i+1)));
ul.appendChild(newLi);
i++;
}
btnRemove.onclick=function() {
if(ul.children.length==0){
return;
}
var lastChild=ul.lastElementChild;
ul.removeChild(lastChild);
i--;
}
</script>
</body>
</html>1回答
好帮手慕码
2020-03-04
同学你好,代码效果正确。继续加油,祝学习愉快~
相似问题