美文网首页让前端飞Web前端之路
pure Javascript implementation o

pure Javascript implementation o

作者: 柳正来 | 来源:发表于2020-04-01 10:28 被阅读0次

    When I was writing bookmarklet I found that I can't use those handy Chrome Console APIs like $, $$, copy, so I have to implement them myself.

    $

    This is similar to document.querySelector but $ supports the second optional parameter startNode (see here) but document.querySelector does not. Instead, we need to use element.querySelector (see here).

    var $ = (selector, startNode) => (startNode || document).querySelector(selector);
    

    $$

    This is similar to document.querySelectorAll.

    One caviat is that the returned value is of type NodeList. It supports APIs like forEach but doesn't support map as Array does. One way to get around it is to wrap it in Array.from.

    var $$ = (selector, startNode) => Array.from((startNode || document).querySelectorAll(selector));
    

    copy

    See my other post

    相关文章

      网友评论

        本文标题:pure Javascript implementation o

        本文链接:https://www.haomeiwen.com/subject/lidouhtx.html