查找选择器(Query Selector)

看到Github上的一个项目:

You Don’t Need jQuery

确实我们现在的开发环境如作者所说的已经改善了很多,目前已经出了很多新的节点查询方法。兼容到现代浏览器与IE10+。

大部分可以使用document.querySelectory(‘selector’)/document.querySelectorAll(‘selector’);

  • document.querySelector 返回第一个值
  • document.querySelectorAll 返回节点列表,可以通过[].slice.call(document.querySelectorAll(selector) || []); 转为数组。
  • 1.0 Query by selector
    // jQuery
    $('selector');
    
    // Native
    document.querySelectorAll('selector');
  • 1.1 Query by class
    // jQuery
    $('.class');
    
    // Native
    document.querySelectorAll('.class');
    
    // or
    document.getElementsByClassName('class');
  • 1.2 Query by id
    // jQuery
    $('#id');
    
    // Native
    document.querySelector('#id');
    
    // or
    document.getElementById('id');
  • 1.3 Query by attribute
    // jQuery
    $('a[target=_blank]');
    
    // Native
    document.querySelectorAll('a[target=_blank]');
  • 1.4 Find sth.
    • Find nodes
      // jQuery
      $el.find('li');
      
      // Native
      el.querySelectorAll('li');
    • Find body
      // jQuery
      $('body');
      
      // Native
      document.body;
    • Find Attribute
      // jQuery
      $el.attr('foo');
      
      // Native
      e.getAttribute('foo');
    • Find data attribute
      // jQuery
      $el.data('foo');
      
      // Native
      // using getAttribute
      el.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
      // jQuery
      $el.siblings();
      
      // Native
      [].filter.call(el.parentNode.children, function(child) {
        return child !== el;
      });
    • Previous elements
      // jQuery
      $el.prev();
      
      // Native
      el.previousElementSibling;
    • Next elements
      // jQuery
      $el.next();
      
      // Native
      el.nextElementSibling;
  • 1.6 ClosestReturn the first matched element by provided selector, traversing from current element to document.
    // jQuery
    $el.closest(selector);
    
    // Native - Only latest, NO IE
    el.closest(selector);
    
    // Native - IE10+ 
    function 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 UntilGet the ancestors of each element in the current set of matched elements, up to but not including the element matched by the selector, DOM node, or jQuery object.
    // jQuery
    $el.parentsUntil(selector, filter);
    
    // Native
    function parentsUntil(el, selector, filter) {
      const result = [];
      const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
    
      // match start from parent
      el = 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
      // jQuery
      $('#my-input').val();
      
      // Native
      document.querySelector('#my-input').value;
    • Get index of e.currentTarget between .radio
      // jQuery
      $(e.currentTarget).index('.radio');
      
      // Native
      [].indexOf.call(document.querySelectAll('.radio'), e.currentTarget);
  • 1.9 Iframe Contents$('iframe').contents() returns contentDocument for this specific iframe
    • Iframe contents
      // jQuery
      $iframe.contents();
      
      // Native
      iframe.contentDocument;
    • Iframe Query
      // jQuery
      $iframe.contents().find('.css');
      
      // Native
      iframe.contentDocument.querySelectorAll('.css');

今天先学习这个节点查询,后面继续学习其他部分以及探讨兼容性问题。


关注我

我的微信公众号:前端开发博客,在后台回复以下关键字可以获取资源。

  • 回复「小抄」,领取Vue、JavaScript 和 WebComponent 小抄 PDF
  • 回复「Vue脑图」获取 Vue 相关脑图
  • 回复「思维图」获取 JavaScript 相关思维图
  • 回复「简历」获取简历制作建议
  • 回复「简历模板」获取精选的简历模板
  • 回复「加群」进入500人前端精英群
  • 回复「电子书」下载我整理的大量前端资源,含面试、Vue实战项目、CSS和JavaScript电子书等。
  • 回复「知识点」下载高清JavaScript知识点图谱

每日分享有用的前端开发知识,加我微信:caibaojian89 交流