CSS 中定义的可见性值未显示在 Javascript 中
Visibility values defined in CSS not showing in Javascript
以下代码生成预期的结果:
<script>
function Show_Visibility(){
p = document.getElementById("i_button_2");
alert(p.style.visibility);
}
</script>
<INPUT TYPE = "button" id = "i_button_1" value = "This is Button 1" onclick="Show_Visibility()">
<br>
<INPUT TYPE = "button" id = "i_button_2" value = "This is Button 2">
<script>
p = document.getElementById("i_button_2");
p.style.visibility = "hidden";
</script>
但是,如果我们将按钮 2 的可见性定义为在 CSS 中隐藏,则在加载页面时按钮确实是隐藏的,但 Javascript 警报消息是空白的。
#i_button_2{visibility: hidden;}
<script>
function Show_Visibility(){
p = document.getElementById("i_button_2");
alert(p.style.visibility);
}
</script>
<INPUT TYPE = "button" id = "i_button_1" value = "This is Button 1" onclick="Show_Visibility()">
<br>
<INPUT TYPE = "button" id = "i_button_2" value = "This is Button 2">
为什么会这样?
您应该使用 getComputedStyle
通过样式表应用样式:
alert(getComputedStyle(p).visibility);
来自MDN文章:
从getComputedStyle返回的对象是只读的,可用于检查元素的样式(包括由元素或外部样式表设置的样式)。elt.style 对象应该用于设置特定元素的样式。
相关文章:
- 如果函数返回True,则显示Javascript按钮
- 背景图像前显示Javascript警报
- 为我的rails应用程序显示javascript弹出消息
- 如何在Javascript中显示Javascript变量
- 如何在colorbox中显示JavaScript变量
- Xamarin iOS WKWebView显示JavaScript警报
- GoogleMapsV3api只显示Javascript循环中的一些标记
- 如何在Asp.net标签控件中显示Javascript函数值
- 如何在对话框中显示javascript变量
- 在asp.net页面中显示javascript执行过程中的加载图标
- 在html中显示javascript变量won'不起作用
- 如何使用按钮在对话框上显示 Javascript 函数的结果
- 打开新窗口进行打印时不显示 JavaScript 图像
- 显示javascript控制台中的消息
- 在加载html网页时显示JavaScript表
- 在Notepad++中更好地突出显示Javascript语法
- 通过php显示javascript内容
- 文本字段未显示javascript函数中的变量
- 显示Javascript表单验证消息
- VB.NET-WebBrowser没有't在html源代码中显示javascript创建的html元素