如何更改CSS属性“display”在javascript中 [英] How do I change the CSS property "display" in javascript
问题描述
如何在JavaScript中从 display:none
更改CSS属性 display
c> display:normal 这些div?
How do I change the CSS property display
, in JavaScript, from display:none
to display:normal
for these divs?
#hide_0 { display:none }
#hide_1 { display:none }
#hide_2 { display:none }
#hide_3 { display:none }
#hide_4 { display:none }
#hide_5 { display:none }
一次只能有一个。
我使用:
var persistent_element='hide_1';
function link_update(link_display)
{
var local_element;
local_element=document.getElementById(persistent_element);
local_element.style.display='none';
local_element=document.getElementById(link_display);
local_element.style.display='block';
persistent_element=link_display;
}
我如何连接它:m4是一个缩小 - 连接onclick到这些方法
How I connected it : m4 is a minified - connects onclick to these methods
m4('l1',function {return link_update(hide_1);});
m4('l2',function {return link_update(hide_2);});
m4('l3',function {return link_update(hide_3);});
m4('l4',function {return link_update(hide_4);});
m4('l5',function {return link_update(hide_5);});
m4('l6',function {return link_update(hide_6);});
推荐答案
要使用javascript来更改样式,它像这样:
To use javascript to change the style, you can do it like this:
// hide an element
document.getElementById("hide_0").style.display = "none";
// show a block element
document.getElementById("hide_1").style.display = "block";
// to go back to the default or CSS specified value
document.getElementById("hide_2").style.display = "";
所以,如果你想隐藏所有并显示一个,你可以使用这个函数: / p>
So, if you wanted to hide all and show one, you could do that with this function:
function showOneHideOthers(base, len, numToShow) {
// objects must have ids like base_0, base_1, etc...
for (var i = 0; i < len; i++) {
if (i != numToShow) {
document.getElementById(base+i).style.display = "none";
}
}
document.getElementById(base+numToShow).style.display = "block";
}
showOneHideOther("hide_", 6, 2);
normal
不是display属性的有效值。典型值为 block
, none
和 inline
是 inline-block
,表
等等。
P.S. normal
is not a valid value for the display property. The typical values are block
, none
and inline
and there are others like inline-block
, table
, etc....
这篇关于如何更改CSS属性“display”在javascript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!