What is the native equivalent to jQuery's “$.fn.has”?

后端 未结 3 1233
情歌与酒
情歌与酒 2021-01-27 11:51

What\'s the native equivalent of jQuery\'s $.fn.has?

For example, how would you write the following code:

$(\"li\").has(\"ul\").css(\"background-color\",         


        
3条回答
  •  情书的邮戳
    2021-01-27 12:31

    I know this is an extremely late answer but here is an updated version of Lennholm's answer.

    const has = (selector, sub) => {
      const matches = Array.from(document.querySelectorAll(selector));
      return matches.filter(match => match.querySelector(sub) !== null);
    };
    
    has("li", "ul").forEach(li => {
      li.style["background-color"] = "red";
    });
    

提交回复
热议问题