Making a short alias for document.querySelectorAll

前端 未结 9 1670
面向向阳花
面向向阳花 2020-12-12 14:48

I\'m going to be running document.querySelectorAll() a whole lot, and would like a shorthand alias for it.

var queryAll = document.querySelectorAll

queryAll         


        
9条回答
  •  生来不讨喜
    2020-12-12 15:30

    I took @David Muller's approach and one-lined it using a lambda

    let $ = (selector) => document.querySelector(selector);
    let $all = (selector) => document.querySelectorAll(selector);
    

    Example:

    $('body');
    // ...
    

提交回复
热议问题