2012-07-12 56 views
1

下面是我的jQuery的一個片段,我抓取一個下拉列表並試圖從中獲取內聯寬度樣式。我的問題是,在Safari我我不得到一個225在Safari上返回不一致的jQuery CSS寬度屬性

targetSelect的newWidthr_select,所以我不明白爲什麼我的Safari將返回不同的寬度比Firefox,例如。

// get the current select box 
targetSelect = $(this); 
newWidth = parseInt(targetSelect.css('width')); 

我的標記:

<select name="r_select" id="r_select" style="width: 225px; height: 50px; float: left;"> 
    <option>Mickelson</option> 
    <option>Montgomerie</option> 
    <option>Watson</option> 
    <option>Casey</option> 
</select> 

我要補充,我不是有這個下拉同樣的問題:

<select id="r_select_overlay" class="r_select_overlay_test no_style" name="r_select_overlay" style="width: 145px; height: 50px; float:left;"> 
     <option>Mickelson</option> 
     <option>Montgomerie</option> 
     <option>Watson</option> 
     <option>Casey</option> 
</select> 
+0

你試圖看到的'newWidth = targetSelect.width();'給?雖然這看起來很奇怪,但我同意。 – 2012-07-12 09:52:36

+0

是的,它返回相同的值。 – crmpicco 2012-07-12 09:58:14

回答

2

嘗試讓outerWidth而不是簡單的寬度,看看這樣可行。

http://api.jquery.com/outerWidth/

+0

優秀,固定它! 'newWidth = targetSelect.outerWidth();' – crmpicco 2012-07-12 10:04:03

+0

還有一件事你可以重置你的CSS,以便不同的瀏覽器默認設置不再幹擾 – 2012-07-12 10:37:36