用js动态增加的控件如何添加onclick方法?
<scripttype="text/javascript">functionadd(e){varcontainer=document.getElementById('co...
<script type="text/javascript">
function add(e){
var container = document.getElementById('content');
var text = document.getElementById(e).innerHTML;
var newNode = document.createElement("span");
newNode.innerHTML = text;
container.appendChild(newNode);
var ui =document.getElementById(e);
ui.style.visibility="hidden";
}
</script>
</head>
<body>
<div style="border:1px #66FFFF solid;font-family:'微软雅黑'">
<h3 style="font-family:'微软雅黑'">我的爱好</h3>
<div>
<p >先从你喜欢的东西开始,让别人了解你。</p>
<div >
<p>通过单击以下标签进行选择:<a href="#" title="换一组标签" >换一下</a></p>
<div id="option">
<span id="0_杨澜" onclick="add(this.id)">杨澜</span>
<span id="0_马艳丽" onclick="add(this.id)">马艳丽</span>
</div>
</div>
<div id="content" style="width:400px;height:200px;border:2px #99CCFF solid ">
</div>
</div>
</body>
点击标签后,下面的div中增加,如何删除增加的标签呢?写不了onclick方法,求大神帮忙修改下 展开
function add(e){
var container = document.getElementById('content');
var text = document.getElementById(e).innerHTML;
var newNode = document.createElement("span");
newNode.innerHTML = text;
container.appendChild(newNode);
var ui =document.getElementById(e);
ui.style.visibility="hidden";
}
</script>
</head>
<body>
<div style="border:1px #66FFFF solid;font-family:'微软雅黑'">
<h3 style="font-family:'微软雅黑'">我的爱好</h3>
<div>
<p >先从你喜欢的东西开始,让别人了解你。</p>
<div >
<p>通过单击以下标签进行选择:<a href="#" title="换一组标签" >换一下</a></p>
<div id="option">
<span id="0_杨澜" onclick="add(this.id)">杨澜</span>
<span id="0_马艳丽" onclick="add(this.id)">马艳丽</span>
</div>
</div>
<div id="content" style="width:400px;height:200px;border:2px #99CCFF solid ">
</div>
</div>
</body>
点击标签后,下面的div中增加,如何删除增加的标签呢?写不了onclick方法,求大神帮忙修改下 展开
2个回答
展开全部
function add(e){
var container = document.getElementById('content');
var text = document.getElementById(e).innerHTML;
var newNode = document.createElement("span");
newNode.innerHTML = text;
container.appendChild(newNode);
var ui =document.getElementById(e);
ui.style.visibility="hidden";
// 加上这段就可以了
newNode.onclick = function(){
container.removeChild(this);
ui.style.visibility="visible";
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询