[英]get width for a div in jquery , whose width is auto
我想獲得一個div的寬度, 其寬度設置為auto
我試過了 :
$(".my_class").outerWidth(true);
$(".my_class").width();
兩者都顯示0寬度,我不知道為什么。 還有其他選擇嗎?
我認為目前尚不可能。 至少不是在IE之外的任何其他瀏覽器中。 IE實現了element.currentStyle,它表示在CSS文件中編寫的樣式。 另一方面,其余的瀏覽器都實現window.getComputedStyle,它返回這些樣式的計算值。 那就是您在那里收到的,一個數字值而不是自動值。
唯一的解決方法是從document.styleSheets中解析CSS聲明。
References:
http://msdn.microsoft.com/en-us/library/ms535231(VS.85).aspx
https://developer.mozilla.org/en/DOM:window.getComputedStyle
https://developer.mozilla.org/en/CSS/computed_value
我添加了HTML DOM方法(替代方法)來標識元素的寬度。
var x = document.getElementsByClassName(".my_class"); //Fetching the div element
alert(x[0].style.width); //Fetching the width of the selected div element.
我假設只有1個div,其類別名稱為“ my_class ”。 如果有很多,則必須對檢索到的列表進行某些處理以標識特定元素。
這也可能對您不起作用,並且作為最佳實踐,應確定問題的根本原因。
cur_element = document.getElementById('my_div'); console.log('width: ' + cur_element.clientWidth);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.