为什么这个style.display无法工作? [英] why won't this style.display work?

查看:146
本文介绍了为什么这个style.display无法工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这行不通?应该检查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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆