如何让再次添加的li得到样式的改变
我全部删除了再增加三个,样式不触发了。请问如何让添加的li触发onmouseover 和onmouseout
<!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">
const list = document.getElementById('list');
const btnAdd = document.getElementById('btnAdd');
const btnRemove = document.getElementById('btnRemove');
btnAdd.onclick = function () {
const li = document.createElement('li');
li.textContent = '我是li' + (list.childElementCount + 1);
list.appendChild(li);
};
btnRemove.onclick = function () {
list.removeChild(list.lastElementChild);
};
const lis = document.getElementsByTagName('li');
console.log(lis);
for (let i = 0; i < 3; i++) {
;
lis[i].onmouseover = function () {
this.style.background = 'purple';
};
lis[i].onmouseout = function () {
this.style.background = 'green';
};
}
</script>
</body>
</html>
正在回答
如果想要实现新增加的li设置样式的话,需要使用函数进行封装,如下:
<!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: #00c4ff7a;
margin: 0 auto;
}
ul {
width: 300px;
height: 100%;
background-color: #f9c3e6d6;
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 box = document.getElementById('box');
var ulList = document.getElementById('list');
var btnAdd = document.getElementById('btnAdd');
var btnRemove = document.getElementById('btnRemove');
var i = 3;
changeBgc();
addLi();
removeLi();
// 新增加的li添加鼠标样式
ulList.onmouseover = function(){
changeBgc();
}
function changeBgc() {
for (var z = 0; z < 3; z++) {
ulList.children.item(z).onmouseover = function() {
this.style.background = 'blue';
}
ulList.children.item(z).onmouseout = function() {
this.style.background = 'red';
}
}
}
function addLi() {
btnAdd.onclick = function() {
var li = document.createElement('li');
var txt = document.createTextNode('我是li' + (i + 1));
li.appendChild(txt);
ulList.appendChild(li);
i++;
}
}
function removeLi() {
btnRemove.onclick = function() {
if (ulList.children.length == 0) {
return;
}
var lastChild = ulList.lastElementChild
ulList.removeChild(lastChild);
i--;
}
}
</script>
</body>
</html>希望可以帮到你!
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星