Javascript JS切换DIV和x27中的更改;s CSS在第一次单击时不触发
Background我有3个ASP按钮,每个按钮都使用DIV ID参数调用相同的JavaScript。启动时,JS应该切换ID已传递的DIV的display属性 问题第一次单击按钮时,不会发生任何事情。在随后的单击中,一切似乎都很好:如果DIV为“block”,则设置为“none”,反之亦然 代码 对于按钮:Javascript JS切换DIV和x27中的更改;s CSS在第一次单击时不触发,javascript,html,css,Javascript,Html,Css,Background我有3个ASP按钮,每个按钮都使用DIV ID参数调用相同的JavaScript。启动时,JS应该切换ID已传递的DIV的display属性 问题第一次单击按钮时,不会发生任何事情。在随后的单击中,一切似乎都很好:如果DIV为“block”,则设置为“none”,反之亦然 代码 对于按钮: <button id="pdp_section_a_button" Class="pdp_section_button" onclick="Show_Hide_Display
<button id="pdp_section_a_button" Class="pdp_section_button" onclick="Show_Hide_Display('<%=pdp_section_a_div.ClientID%>');return false">Section A</button>
<button id="pdp_section_b_button" Class="pdp_section_button" onclick="Show_Hide_Display('<%=pdp_section_b_div.ClientID%>');return false">Section B</button>
<button id="pdp_section_c_button" Class="pdp_section_button" onclick="Show_Hide_Display('<%=pdp_section_c_div.ClientID%>');return false">Section C</button>
A节
B节
C节
对于JS函数:
<script type="text/javascript">
function Show_Hide_Display(divID) {
alert(document.getElementById(divID).style.display); // on first click this is blank, on other clicks the DIV's current display property is shown
var div = document.getElementById(divID);
if (div.style.display == "" || div.style.display == "block") {
div.style.display = "none";
}
else {
div.style.display = "block";
}
return false;
}
</script>
功能显示\隐藏\显示(divID){
警报(document.getElementById(divID.style.display);//第一次单击此选项为空,其他单击则显示DIV的当前显示属性
var div=document.getElementById(divID);
if(div.style.display==“| | div.style.display==”块”){
div.style.display=“无”;
}
否则{
div.style.display=“块”;
}
返回false;
}
元素。style
仅显示内联样式,而不显示活动css属性
为了获得活动css,请使用以下命令
var div = document.getElementById(divID);
var style = document.defaultView.getComputedStyle(div);
// equivalent to window.getComputedStyle
var display = style.getPropertyValue('display');
if (display == '' || display == 'block') {
div.style.display = 'none';
} else {
div.style.display = 'block';
}
element.style
仅显示内联样式,而不显示活动css属性
为了获得活动css,请使用以下命令
var div = document.getElementById(divID);
var style = document.defaultView.getComputedStyle(div);
// equivalent to window.getComputedStyle
var display = style.getPropertyValue('display');
if (display == '' || display == 'block') {
div.style.display = 'none';
} else {
div.style.display = 'block';
}
div.style
将读取inline
样式,而不是与元素绑定的所有css样式属性。使用窗口。getComputedStyle
div.style
将读取内联
样式,而不是与元素绑定的所有css样式属性。使用window.getComputedStyle