1

我有一个 JavaScript 来打开一个 DIV 作为弹出窗口。但是现在我想从不同的 DIVS 打开几个不同的弹出窗口,我无法让它正常工作。

我使用以下 JavaScript:

<script>
window.onload = function()
{
    var show = document.getElementById('show-div');
    var hide = document.getElementById('hide-div');
    var popup = document.getElementById('popup-div');

    show.onclick = function(e)
    {
        popup.style.visibility = 'visible';
        return false;
    }

    hide.onclick = function(e)
    {
        popup.style.visibility = 'hidden';
        return false;
    }
}
</script>

对于 div,我使用以下代码:

<a id="show-div" href="#">link</a>
<div id="popup-div">
<div id="popup-header">
<div id="popup-title">...Popup title...</div>
<a id="hide-div" href="#">X</a></div>
<div id="popup-body">
<p>This is the content of the div....</p>
</div>
</div>

这适用于 1 个链接,但即使我使用不同的 DIV ID(以及关联的 var 元素,如“var popup1 = document.getElementById('popup-div1');”和 popup1 onclick 事件),我也无法正常工作。我怎样才能使它与具有所有内容的自己的 DIV 的多个链接一起工作。

(因为我无法回答我自己的问题,这是我修复它的方法)我使用以下代码使其工作:

<script>
window.onload = function()
{
    var show = document.getElementById('show-div');
    var show2 = document.getElementById('show-div2');
    var hide = document.getElementById('hide-div');
    var hide2 = document.getElementById('hide-div2');
    var popup = document.getElementById('popup-div');
    var popup2 = document.getElementById('popup-div2');

    show.onclick = function(e)
    {
        popup.style.visibility = 'visible';
        return false;
    }

    show2.onclick = function(e)
    {
        popup2.style.visibility = 'visible';
        return false;
    }

    hide.onclick = function(e)
    {
        popup.style.visibility = 'hidden';
        return false;
    }
    hide2.onclick = function(e)
    {
        popup2.style.visibility = 'hidden';
        return false;
    }
}
</script>

<a id="show-div" href="#">link</a>
<div id="popup-div">
<div id="popup-header">
<div id="popup-title">...Popup title...</div>
<a id="hide-div" href="#">X</a></div>
<div id="popup-body">
<p>This is the content of the div....</p>
</div>
</div>
<a id="show-div2" href="#">link2</a>
<div id="popup-div2">
<div id="popup-header">
<div id="popup-title">...Popup title2...</div>
<a id="hide-div2" href="#">X</a></div>
<div id="popup-body">
<p>This is the content of the div2....</p>
</div>
</div>
4

1 回答 1

0

我相信以下是你想要的......

演示

JS代码:

var show = document.getElementById('show-div');
var hides = document.getElementsByClassName('hide-div');
var popups = document.getElementsByClassName("popup-div");

show.onclick = function (e) {
    for (var i = 0; i < popups.length; i++) {
        popups[i].style.visibility = 'visible';
    }
    return false;
}

for (var j = 0; j < hides.length; j++) {
    hides[j].onclick = function (e) {
        this.parentNode.parentNode.style.visibility = 'hidden';
        return false;
    }
}

HTML 代码:

<a id="show-div" href="#">Show/Hide</a>

<div id="popup-div" class="popup-div">
    <div id="popup-header">
        <div id="popup-title">...Popup title...</div>
<a id="hide-div" class="hide-div" href="#">X</a>

    </div>
    <div id="popup-body">
        <p>This is the content of the div....</p>
    </div>
</div>
<div id="popup-div" class="popup-div">
    <div id="popup-header">
        <div id="popup-title">...Popup title...</div>
<a id="hide-div" class="hide-div" href="#">X</a>

    </div>
    <div id="popup-body">
        <p>This is the content of the div....</p>
    </div>
</div>
于 2013-10-21T12:49:34.007 回答