• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

为什么style.display没办法工作?

用户头像
it1352
帮助1

问题说明

为什么这行不通?应该检查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   type="text" name="annonsera_price" id="annonsera_price">

我也没有用CSS设置它!

I haven't set it with css either!

正确答案

#1

首先尝试仅警告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.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhccijig
系列文章
更多 icon
同类精品
更多 icon
继续加载