创建的多个DIV分别用按钮关闭怎样实现

WormJan 2012-11-29 07:21:49
就是想弹出多个DIV,每个DIV上有一个按钮,用来关闭这个DIV。
现在的情况是,如果我设置DIV自动移除,用settimeout可以正常移除。也不会发生混乱,一切正常。但是如果用DIV上的按钮来移除自己所在的DIV的话,好像移除的总是最后创建的那个DIV。求大神指点。有测试代码,就差按钮的代码了。为了弹出不同的DIV,我弄了2个按钮,来弹出2个DIV。

<html>
<body>
<script>
function showWindow(title,content,size) {
this.title = title;
this.content = content;
this.size = size;
var date = new Date();
this.objid = date.getHours() + "" + date.getMinutes() + "" + date.getSeconds() + "" + date.getMilliseconds();
}
showWindow.prototype = {
show: function (type, autoClose) {
var divObj = document.createElement("div");
//divObj.id = 'window_' + this.objid;
divObj.style.width = this.size[0] + 'px';
divObj.style.height = this.size[1] + 'px';
//divObj.style.position = "absolute";
divObj.style.border = "1px #ccc solid";
divObj.innerHTML = '<div style="width:10px;height:10px"></div><input type="button" value="关闭" onclick="closeWindow()" style="float:right" />';
document.body.appendChild(divObj);
this.closeWindow = function () {
document.body.removeChild(divObj);
}
if (autoClose > 0) {
setTimeout(function () {
document.body.removeChild(divObj);
}, autoClose * 1000);
}
closeWindow = function () {
document.body.removeChild(divObj);
}
}
}
function test1(){
var size1 = new Array('100', '100');
var s = new showWindow('aaa', 'bbb', size1);
s.show({ 'type': '0' }, 0);
}
function test2(){
var size1 = new Array('150', '150');
var s = new showWindow('ccc', 'ddd', size1);
s.show({ 'type': '0' }, 0);
}
</script>
<input type="button" value="测试1" onclick="test1()" />
<input type="button" value="测试2" onclick="test2()" />
</body>
</html>
...全文
150 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
WormJan 2012-11-29
  • 打赏
  • 举报
回复
非常感谢!可以了。
  • 打赏
  • 举报
回复
divObj.innerHTML = '<div style="width:10px;height:10px"></div><input type="button" value="关闭" onclick="document.body.removeChild(this.parentNode)" style="float:right" />';

87,991

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧