簡體   English   中英

為什么這個style.display不起作用?

[英]why won't this style.display work?

為什么這不起作用? 它應檢查display css樣式的元素。

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設置它!

如果樣式是通過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");

首先嘗試僅提醒style.display並查看它包含的內容:

var el = document.getElementById("annonsera_price");
alert(el.style.display);

style僅引用style屬性中的內容(或在style屬性上通過JS設置)。 顯示是通過類設置的,因此el.style.display不會顯示任何內容。

[ 更新 ]:鑒於更新,沒有警報發生的原因是因為style.display將是“”,而不是“內聯”或“阻止”。 在將它設置為某個內容之前,它不會是“內聯”或“阻止”,無論是在style屬性中還是通過設置.style.display

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM