为什么这个style.display无法工作? [英] why won't this style.display work?
问题描述
为什么这行不通?应该检查display
css样式的元素.
Why won't this work? It should check the element for display
css style.
if (byId("annonsera_price").style.display=='inline' || byId("annonsera_price").style.display=='block'){
alert ("Hello");
}
function byId(x){
return document.getElementById(x);
}
update2:
<input style="width:100px;" type="text" name="annonsera_price" id="annonsera_price">
我也没有用CSS设置它!
I haven't set it with css either!
推荐答案
首先尝试仅警告style.display并查看其包含的内容:
First try alerting just the style.display and see what it contains:
var el = document.getElementById("annonsera_price");
alert(el.style.display);
style
仅引用样式属性中的内容(或通过样式属性上的JS设置).显示可能是通过该类设置的,因此el.style.display
将不会显示任何内容.
style
only references what is in the style attribute (or set through JS on the style property). It's possible that the display is set through the class, and therefore el.style.display
won't show anything.
[更新]:鉴于更新,没有警报发生的原因是style.display
将是",而不是内联"或阻止".除非您在style
属性中或通过设置.style.display
对其进行设置,否则它不会是内联"或阻止".
[Update]:Given the update, the reason no alert happens is because style.display
will be "", not "inline" or "block". It won't be "inline" or "block" until you set it to something, either in the style
attribute or by setting .style.display
.
这篇关于为什么这个style.display无法工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!