日期:2014-05-16  浏览次数:20358 次

创建的多个DIV分别用按钮关闭怎样实现
就是想弹出多个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>

------解决方案--------------------
divObj.