Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 排除《木偶演员》中某些类别的元素_Javascript_Css_Node.js_Google Chrome Devtools_Puppeteer - Fatal编程技术网

Javascript 排除《木偶演员》中某些类别的元素

Javascript 排除《木偶演员》中某些类别的元素,javascript,css,node.js,google-chrome-devtools,puppeteer,Javascript,Css,Node.js,Google Chrome Devtools,Puppeteer,我试图用Puppeter解析的HTML如下所示: <ul> <li class="title"> item 1 </li> <li class="title hide"> item 1 </li> </ul> await page.$$eval("ul > li.title", nodes => nodes.map(element => { return {

我试图用Puppeter解析的HTML如下所示:

<ul>
    <li class="title"> item 1 </li>
    <li class="title hide"> item 1 </li>
</ul>
await page.$$eval("ul > li.title", nodes =>
    nodes.map(element => {
      return {
        //some attributes
      };
    })
  );
扩展的结果是只检索不带
class=hide
的元素。不幸的是,
hide
是在
title
之外的一个类,它由所有
  • 元素共享

    如何重构木偶演员代码以排除带有
    隐藏
    类的元素?

    只需将
    :not(.hide)
    添加到选择器字符串:

    page.$$eval("ul > li.title:not(.hide)", nodes =>
    
    await page.$$eval('ul > li.title', nodes =>
      nodes.filter(e => !e.matches('.hide')).map(element => {
        return {
          // some attributes
        };
      })
    );
    
    :不是(.hide) 您应该使用CSS伪类来选择不包含该类的元素。
    。hide

    await page.$$eval('ul > li.title:not(.hide)', nodes =>
      nodes.map(element => {
        return {
          // some attributes
        };
      })
    );
    
    .filter(e=>!e.matches('.hide')) 另一方面,您也可以使用
    节点
    仅包含不属于
    选择器字符串的元素。隐藏
    选择器字符串:

    page.$$eval("ul > li.title:not(.hide)", nodes =>
    
    await page.$$eval('ul > li.title', nodes =>
      nodes.filter(e => !e.matches('.hide')).map(element => {
        return {
          // some attributes
        };
      })
    );
    

    这太完美了。。有什么地方可以让我了解更多关于
    文档中的内容。querySelector()
    querySelector
    接受任何有效的CSS选择器字符串-请参阅(包括该页面左侧的链接),它们非常灵活