Thinker_Ac
2014-12-12 23:07
为什么加上window.onload = function (){}就不行了呢
这样写的时候可以,onload是加载整个页面时调用函数,需要明确具体是那个元素触发事件时才调用,而onclick=del(this)是每次单独调用,this就代表自己,与别人无关。我是这样理解的
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
li{
width: 100px;
height: 20px;
line-height: 20px;
position: relative;
margin: 10px;
}
button {
position: absolute;
right: 3px;
top: 1px;
height: 20px;
/* 隐藏深处按钮 */
display:none;
}
li:hover button {
/* 显示删除按钮 */
display:block;
}
</style>
<script type="text/javascript">
window.onload=function()
{
var li=document.getElementsByTagName('button');
for(var i=0;i<li.length;i++)
{
li[i].onclick=function()
{
var p = this.parentNode;
p.parentNode.removeChild(p);
};
}
}
</script>
</head>
<body>
<ul>
<li>内容1<button >删除</button></li>
<li>内容2<button >删除</button></li>
<li>内容3<button >删除</button></li>
<li>内容4<button >删除</button></li>
<li>内容5<button >删除</button></li>
</ul>
</body>
</html>人人网评论功能
27691 学习 · 155 问题
相似问题