为什么这个style.display不起作用?

2 html javascript

为什么这不起作用?它应该检查displaycss样式的元素.

if (byId("annonsera_price").style.display=='inline' || byId("annonsera_price").style.display=='block'){
alert ("Hello");
}

function byId(x){
        return document.getElementById(x);
}
Run Code Online (Sandbox Code Playgroud)

UPDATE2:

<input style="width:100px;" type="text" name="annonsera_price" id="annonsera_price">
Run Code Online (Sandbox Code Playgroud)

我也没有用css设置它!

And*_*y E 5

如果样式是通过CSS设置的,或者是默认样式,则需要获取元素的计算样式:

var el    = document.getElementById("annonsera_price");

// currentStyle for IE, getComputedStyle for standards-compliant browsers
var style = el.currentStyle || window.getComputedStyle(el);

if (style.display == "inline" || style.display == "block")
    alert("Hello");
Run Code Online (Sandbox Code Playgroud)