开发者

document.getElementbyId - getting more than one id at once?

开发者 https://www.devze.com 2023-03-26 14:09 出处:网络
Is it possible to hide more than one pop-up in one call? For example, ...onclick=\"document.getElementById(\'PopUp1\').style.display =开发者_开发问答 \'none\' \"...

Is it possible to hide more than one pop-up in one call? For example,

    ...onclick="document.getElementById('PopUp1').style.display =开发者_开发问答 'none' "...

Can I ask it to immediately get the elements with id PopUp2, PopUp3 etc., too? Is this possible with a simple syntax change or not?


No you can't do that in pure JavaScript. You'd better call a function from the onclick event and then loop through the popups in that function:

function closePopups() {
    for (var i = 1; i <= 3; i++) {
        document.getElementById('PopUp' + i).style.display = 'none' 
    }
}

Then your event handler would be:

...onclick="closePopups()"...


This is where selector-based frameworks like jQuery are really, really useful. In jQuery, either of these could be used:

$("#myButton").click(function() {
    $("#Popup1, #Popup2, #Popup3").hide();
});

Or, if you put a common "class=popup" on all these objects that you want hidden, you could use this:

$("#myButton").click(function() {
    $(".popup").hide();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消