简体   繁体   English

document.getElementbyId - 一次获得多个id?

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

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? 我可以要求它立即获取id为PopUp2,PopUp3等的元素吗? Is this possible with a simple syntax change or not? 这是否可以通过简单的语法更改来实现?

No you can't do that in pure JavaScript. 不,你不能用纯JavaScript做到这一点。 You'd better call a function from the onclick event and then loop through the popups in that function: 你最好从onclick事件中调用一个函数,然后遍历该函数中的弹出窗口:

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. 这就是基于选择器的框架(如jQuery)真正非常有用的地方。 In jQuery, either of these could be used: 在jQuery中,可以使用以下任何一个:

$("#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: 或者,如果您在要隐藏的所有这些对象上放置一个共同的"class=popup" ,则可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM