我有一个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