我有一個JQuery(使用JQuery 1.4.2)的問題,只出現在IE8的標準模式下,在一個特定的DOM元素上,但沒有在其他幾乎相同的DOMEMements上。爲什麼它是沒有意義的最好例子如下:IE8標準模式JQuery illogic
$('span.error:visible')[0].style.display
上面這段代碼返回「無」的,除非我有某種腦部動脈瘤是不可能的,而不存在一個bug在任何JQuery的或IE8 。這隻發生在IE8的標準模式下,而不是在任何其他瀏覽器或IE8兼容模式下。它發現的跨度實際上是一個ASP.net驗證控件,所以如果控制它呈現給瀏覽器的內容,我只有有限的數量。當我使用IE8的開發者工具欄檢查DOM和DOM中複製HTML它給了我下面的:
<SPAN style="DISPLAY: none; COLOR: red"
id=ctl00_cphContentBody_mnMainMiddleNames_ebvMiddleName1 class=error
controltovalidate="ctl00_cphContentBody_mnMainMiddleNames_txtMiddleName0"
errormessage="JQuery should not find this" display="Dynamic" validationGroup="MiddleNames"
isvalid="true" validationexpression="[A-Za-z][A-Za-z '\-]*[A-Za-z]*">JQuery should not
find this</SPAN>
如果我只是做了查看源代碼複製並粘貼它,我得到如下:
<span id="ctl00_cphContentBody_mnMainMiddleNames_ebvMiddleName1" class="error"
style="color:Red;display:none;">JQuery should not find this</span>
如果我創建一個簡單的HTML文件,其中只包含上述HTML中的任何一個,那麼$('span.error:visible')
找不到跨度,我無法發佈代碼以便能夠重現此問題。但在實際的asp.net頁面,如果我運行$('span.error:visible')[0].style.display
它返回「無」,如果我運行$('span.error:visible').text()
它返回「JQuery不應該 找到這個」。
tl; dr $('span.error:visible')[0].style.display
如何返回「無」。
編輯回答尼克斯評論。
$('span.error:visible')[0].offsetWidth
回報3個 $('span.error:visible')[0].offsetHeight
收益22
這是令人費解的,我發現了jQuery網站的下方。
In jQuery 1.3.2 an element is visible if its browser-reported offsetWidth or offsetHeight is greater than 0.
該元素不可見,但根據上述JQuery認爲它是。
What does this change mean? It means that if your element's CSS display is "none", or any of its parent/ancestor element's display is "none", or if the element's width is 0 and the element's height is 0 then an element will be reported as hidden.
以上就是錯誤。顯示爲「無」,但offsetWidth和offsetHeight不爲零。
'$('span.error:可見')[0] .offsetWidth'和'$('span.error:visible')[0] .offsetHeight'給你嗎? – 2010-08-25 12:09:01
This h每次都會出現?或者它是間歇性的? – 2010-08-25 12:25:35