类似$(。someClass)的JQuery类选择器区分大小写? [英] JQuery class selectors like $(.someClass) are case sensitive?
问题描述
给出这个HTML:
< div class =OpenIDSelector>一些文字< / div>
为什么这个JQuery选择器在某些浏览器和某些页面上匹配它,但在其他页面上却不匹配?
$('。OpenIdSelector')
注意:我遇到了这个问题并且自己解决了这个问题,但是它很烦人,我没有在StackOverflow上找到它,所以我把它作为Q&一对,所以别人不会像我一样浪费一个小时。
结果JQuery的类选择器使用新的JavaScript方法 getElementsByClassName 如果浏览器支持它。此方法在特殊模式页面上不区分大小写,并且在非quirksmode(又名符合标准)页面上区分大小写。当然,情况通常很明显,但是当文本卡在长而复杂的选择器的中间时,很难看清楚。显然标准和怪癖之间有很多区分大小写的区别需要注意。故事的道德:在你的HTML(元素名称,CSS类等)中匹配 everything 的大小写,因为你永远不知道何时更改为浏览器或标准或库可能会使您对不区分大小写的假设失效。
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.
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.
这篇关于类似$(。someClass)的JQuery类选择器区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!