[英]make one element visible and 49 others invisible
I am absolutely new to javascript, so please bear with me. 我绝对不是javascript的新手,所以请多多包涵。
I have 50 elements on my page with ids. 我的页面上有50个具有ID的元素。 All are set to visibility:hidden and position:fixed. 全部设置为“可见性:隐藏”和“位置:固定”。 I have a button that corresponds to each element. 我有一个对应于每个元素的按钮。 When a button is clicked, a javascript function is initiated which makes the corresponding element visibile and position:relative. 单击按钮后,将启动javascript函数,该函数使相应的元素可见且与position:relative相对。 Code looks something like this: 代码看起来像这样:
document.getElementById("id1").style.position='relative';
document.getElementById("id1").style.visibility='visible';
To ensure that only one element is ever visible and relative, I also need to make the other 49 elements hidden and fixed. 为了确保只有一个元素可见且相对,我还需要隐藏和修复其他49个元素。 How can I accomplish this without having to resort to the following sort of code: 我如何无需借助以下代码即可完成此任务:
function makeid1visibile()
{
document.getElementById("id1").style.position='relative';
document.getElementById("id1").style.visibility='visible';
document.getElementById("id2").style.position='fixed';
document.getElementById("id2").style.visibility='hidden';
document.getElementById("id3").style.position='fixed';
document.getElementById("id3").style.visibility='hidden';
document.getElementById("id4").style.position='fixed';
document.getElementById("id4").style.visibility='hidden';
// etc...
}
Any help would be appreciated, because with 50 elements, the number of lines of coding would be outrageous. 任何帮助将不胜感激,因为使用50个元素,编码的行数将非常大。
Should be able to handle it with a single loop, just pass in the number of the item you wish to show: 应该能够通过一个循环处理它,只需传递您希望显示的项目编号即可:
function makeIdVisible(id) {
document.getElementById("id" + id).style.position='relative';
document.getElementById("id" + id).style.visibility='visible';
for (var i = 1; i <= 50; i++) {
if (i !== id) {
document.getElementById("id" + i).style.position='fixed';
document.getElementById("id" + i).style.visibility='hidden';
}
}
}
给您的复选框类名“ someclass”,并通过函数documet.getElementsByClassName
选择所有元素
You can write a function like this: 您可以编写如下函数:
function makeVisible( id ){
var idList = ['id1','id2','id3','id4'];
for( var i = 0, l = idList.length; i<l ; i++ ){
document.getElementById(idList[i]).style.position='fixed';
document.getElementById(idList[i]).style.visibility='hidden';
}
document.getElementById(id).style.position='relative';
document.getElementById(id).style.visibility='visible';
}
Then you can use makeVisible('#id1'); 然后,您可以使用makeVisible('#id1'); to make the id1 element visible 使id1元素可见
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.