老师帮我检查下代码,还有什么需要改进的吗?
<!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 ul = document.getElementById('list');
var li = ul.querySelectorAll('li');
var btnAdd = document.getElementById('btnAdd');
var btnRemove = document.getElementById('btnRemove');
for(var i = 0; i < li.length; i++){
li[i].onmouseover = function(){
this.style.background = "blue";
}
li[i].onmouseout = function(){
this.style.background = "pink";
}
}
btnAdd.onclick = function(){
var li1 = document.createElement('li');
var len = ul.children.length;
var txt = document.createTextNode('我是li' + (len + 1));
li1.appendChild(txt);
ul.appendChild(li1);
}
btnRemove.onclick = function(){
ul.removeChild(ul.lastElementChild);
}
</script>
</body>
</html>
正在回答 回答被采纳积分+1
<!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 ul = document.getElementById('list');
// var li = ul.querySelectorAll('li');
var li = ul.getElementsByTagName('li');
var btnAdd = document.getElementById('btnAdd');
var btnRemove = document.getElementById('btnRemove');
function changeColor() {
for(var i = 0; i < li.length; i++){
if (i >= 3) {
return;
}
li[i].onmouseover = function(){
this.style.background = "blue";
}
li[i].onmouseout = function(){
this.style.background = "pink";
}
}
}
changeColor();
btnAdd.onclick = function(){
var li1 = document.createElement('li');
var len = ul.children.length;
var txt = document.createTextNode('我是li' + (len + 1));
li1.appendChild(txt);
ul.appendChild(li1);
changeColor();
}
btnRemove.onclick = function(){
var len = ul.children.length;
if (len < 1) {
return;
}
ul.removeChild(ul.lastElementChild);
}
</script>
</body>
</html>
老师 var li = ul.querySelectorAll('li');
和var li = ul.getElementsByTagName('li');实现效果一样,用哪个都行吧?
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星