像 $(.someClass) 这样的 JQuery 类选择器区分大小写吗?
给定这个 HTML:
<div class="OpenIDSelector">some text</div>
为什么这个 JQuery 选择器在某些浏览器和某些页面上匹配它,但在其他浏览器和某些页面上不匹配?
$('.OpenIdSelector')
注意:我遇到了这个问题并自己解决了它,但这很烦人,而且我还没有在 StackOverflow 上找到它,所以我将其作为问答对发布,这样其他人就不会浪费时间像我一样小时。
Given this HTML:
<div class="OpenIDSelector">some text</div>
Why does this JQuery selector match it on some browsers and some pages, but not on others?
$('.OpenIdSelector')
NOTE: I ran into this problem and solved it myself, but it was annoying and I didn't find it on StackOverflow already, so I'm posting it as a Q&A pair so someone else won't waste an hour like I did.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结果JQuery的类选择器使用了新的javascript方法getElementsByClassName(如果浏览器支持)。此方法在怪异模式页面上不区分大小写,在非怪异模式(也称为符合标准)页面上区分大小写。当然,通常情况下情况不同是显而易见的,但是当文本卡在又长又复杂的选择器中间时,就很难看到。显然,标准和怪癖之间存在许多区分大小写的差异,需要注意。
这个故事的寓意:匹配 HTML 中所有内容的大小写(元素名称、CSS 类等),因为您永远不知道浏览器、标准或库的更改何时可能会使您对大小写的假设失效 -不敏感。
Turns out JQuery's class selector uses the new javascript method getElementsByClassName if the browser supports it. This method is case-insensitive on quirks-mode pages, and case-sensitive on non-quirksmode (aka standards-compliant) pages. Sure, it's usually obvious that the cases are different, but when the text is stuck in the middle of a long, complex selector it was hard to see. Apparently there are lots of case-sensitive differences between standards and quirks to watch out for.
Moral of the story: match case of everything in your HTML (element names, CSS classes, etc.) because you never know when a change to a browser or standard or library might invalidate your assumption about case-insensitivity.