+1 for pointing out the live node list distinction.

I heard several times that jQuery's strongest asset is the way it queries and manipulates elements in the DOM: you can use CSS queries to create complex queries that would be very hard to do in regular javascript . If you don't want to use all of jQuery, you could just use Sizzle separately.

To learn more, see our tips on writing great answers. querySelectorAll will return a static collection, while getElementsByClassName returns a live collection. In this sense, 5 lines of code is better than 10. From a performance perspective, that would double the amount of work for every selector. Ugh, I had to replace jQuery in our app with a work-a-like jQuery that I wrote. getElement* will still find elements in roughly the same amount of time regardless of their placement. Perhaps you would like to share what it is about my answer that offends you so much. The NodeList object represents a collection of nodes. :) I made a module exactly for these purposes just recently: gimme back my dollars! Using getElement* on a static element fetched with querySelector* will result in an element that is live with respect to the static subset of the DOM copied by querySelector, but not live with respect to the full document DOM... this is where the simple live/static interpretation of elements begins to fall apart.
That's an extremely important difference to be aware of depending on how you intend to use the results. Please, please, PLEASE provide better information. What is “not even correct”. Thanks!
When support for older browsers, such as Internet Explorer 8 and below, is necessary, consider the following tips: Use your code for good and have fun! If you still are interested in pursuing this issue, feel free to add a comment with the requested information and we will be happy to reopen the ticket if it is still valid. Are insectivores and carnivores viable to become sapient? 获取文档中所有的

元素, 并为匹配的第一个

元素 (索引为 0) 设置背景颜色: 获取文档中所有 class="example" 的

元素, 并为匹配的第一个

元素 (索引为 0) 设置背景颜色: 计算文档中 class="example" 的

元素的数量(使用 NodeList 对象的 length 属性): HTML DOM 参考手册: element.querySelectorAll(), 必须。 指定一个或多个匹配 CSS 选择器的元素。可以通过 id, class, 类型, 属性, 属性值等作为选择器来获取元素。, 一个 NodeList 对象,表示文档中匹配指定 CSS 选择器的所有元素。 NodeList 是一个静态的 NodeList 类型的对象。如果指定的选择器不合法,则抛出一个 SYNTAX_ERR 异常。. Though it is generally easy to tell if a selector can be written using only getElement* calls, there is one case that may not be obvious: document.querySelectorAll(".class1.class2"), document.getElementsByClassName("class1 class2"). As a rule, I caution against using jQuery for new projects: If none of the above matters, then do what you will. It can be used like this: "Authors are advised that while the use of pseudo-elements in selectors is permitted, they will not match any elements in the document, and thus would not result in any elements being returned. The : character has special meaning inside a selector. very precise should be put in your blog, Sasha, "Changes to static elements are only written back to the DOM after the current script is done executing." @Christoph Since this is easy, I added compatiblity for IE8 and above. @user1032663 that's a matter of documentation-conventions. Chrome has its own Console Utilities API Reference where $ and $$ are offered regardless, among other utils, unless the global scope has been overwritten with something else (i.e.