常用的 class、id、属性 选择器都可以使用 document.querySelector
或 document.querySelectorAll
替代。区别是
document.querySelector
返回第一个匹配的 Elementdocument.querySelectorAll
返回所有匹配的 Element 组成的 NodeList。它可以通过[].slice.call()
把它转成 Array如果匹配不到任何 Element,jQuery 返回空数组
[]
,但document.querySelector
返回null
,注意空指针异常。当找不到时,也可以使用||
设置默认的值,如document.querySelectorAll(selector) || []
注意:
document.querySelector
和document.querySelectorAll
性能很差。如果想提高性能,尽量使用document.getElementById
、document.getElementsByClassName
或document.getElementsByTagName
。
1.0 Query by selector
12345// jQuery$('selector');// Nativedocument.querySelectorAll('selector');1.1 Query by class
12345678// jQuery$('.css');// Nativedocument.querySelectorAll('.css');// ordocument.getElementsByClassName('css');1.2 Query by id
12345678// jQuery$('#id');// Nativedocument.querySelector('#id');// ordocument.getElementById('id');1.3 Query by attribute
12345// jQuery$('a[target=_blank]');// Nativedocument.querySelectorAll('a[target=_blank]');1.4 Find sth.
Find nodes
12345// jQuery$el.find('li');// Nativeel.querySelectorAll('li');Find body
12345// jQuery$('body');// Nativedocument.body;Find Attribute
12345// jQuery$el.attr('foo');// Nativee.getAttribute('foo');Find data attribute
12345678// jQuery$el.data('foo');// Native// using getAttributeel.getAttribute('data-foo');// you can also use `dataset` if only need to support IE 11+el.dataset['foo'];
1.5 Sibling/Previous/Next Elements
Sibling elements
1234567// jQuery$el.siblings();// Native[].filter.call(el.parentNode.children, function(child) {return child !== el;});Previous elements
12345// jQuery$el.prev();// Nativeel.previousElementSibling;Next elements
123// next$el.next();el.nextElementSibling;
1.6 Closest
Closest 获得匹配选择器的第一个祖先元素,从当前元素开始沿 DOM 树向上。
12345678910111213141516// jQuery$el.closest(queryString);// Nativefunction closest(el, selector) {const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;while (el) {if (matchesSelector.call(el, selector)) {return el;} else {el = el.parentElement;}}return null;}1.7 Parents Until
获取当前每一个匹配元素集的祖先,不包括匹配元素的本身。
12345678910111213141516171819202122// jQuery$el.parentsUntil(selector, filter);// Nativefunction parentsUntil(el, selector, filter) {const result = [];const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;// match start from parentel = el.parentElement;while (el && !matchesSelector.call(el, selector)) {if (!filter) {result.push(el);} else {if (matchesSelector.call(el, filter)) {result.push(el);}}el = el.parentElement;}return result;}1.8 Form
Input/Textarea
12345// jQuery$('#my-input').val();// Nativedocument.querySelector('#my-input').value;Get index of e.currentTarget between
.radio
12345// jQuery$(e.currentTarget).index('.radio');// Native[].indexOf.call(document.querySelectorAll('.radio'), e.currentTarget);
1.9 Iframe Contents
jQuery 对象的 iframe
contents()
返回的是 iframe 内的document
Iframe contents
12345// jQuery$iframe.contents();// Nativeiframe.contentDocument;Iframe Query
12345// jQuery$iframe.contents().find('.css');// Nativeiframe.contentDocument.querySelectorAll('.css');
本文作者:余震(Freak)
本文出处:Rockjins Blog
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN许可协议。转载请注明出处!