这样做可以吗?
来源:4-10 自由编程
慕前端5592812
2019-07-31 10:56:38
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
</style>
</head>
<body>
<button type="button">添加</button>
<ul>
<li>001</li>
<li>002</li>
<li>003</li>
<li>004</li>
</ul>
<script type="text/javascript" src="lib/zepto.min.js"></script>
<script>
var $li = $("<li>005</li>");
$(document).ready(function () {
$('ul').on('mouseover','li',function () {
$(this).css("backgroundColor",'red').mouseleave(function () {
$(this).css('backgroundColor','white');
});
})
$('button').click(function () {
$('li:last-child').append($li);
})
})
</script>
</body>
</html>
1回答
好帮手慕慕子
2019-07-31
同学你好, 如下图所示, 新添加的li是004的子元素了, 会导致鼠标鼠标移入004的时候,新添加的005背景颜色也发生改变

建议修改: 可以直接给ul追加子元素即可

如果帮助到了你, 欢迎采纳!
祝学习愉快~~~~
相似问题