Making a short alias for document.querySelectorAll

前端 未结 9 1657
面向向阳花
面向向阳花 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:34

    The JavaScript interpreter throws an error because querySelectorAll() should be invoked in document context.

    The same error is thrown when you are trying to call console.log() aliased.

    So you need to wrap it like this:

     function x(selector) {
         return document.querySelectorAll(selector);
     }
    

提交回复
热议问题