find and select “li” that didn't contain a button

问题: I want to get the number of "li" that didn't contain button inside of it at any child I tried to use filter, :not, :contains, queryselector and others ways but any of th...

问题:

I want to get the number of "li" that didn't contain button inside of it at any child

I tried to use filter, :not, :contains, queryselector and others ways but any of this give the desired result.

<ul class="jSC57">
  <div class="PZuss">
    <li>
      <div>
        <p>test1</p>
      </div>
      <div>
        <button class="_0mzm- sqdOP  L3NKy   _8A5w5" type="button">Demandé</button>
      </div>
    </li>
    <li>
      <div>
        <p>test2</p>
      </div>
      <div>
        <button class="_0mzm- sqdOP  L3NKy   _8A5w5" type="button">Demandé</button>
      </div>
    </li>
    <li>
      <div>
        <p>test3</p>
      </div>
      <div>
      </div>
    </li>
    <li>
      <div>
        <p>test4</p>
      </div>
      <div>
        <button class="_0mzm- sqdOP  L3NKy   _8A5w5" type="button">Demandé</button>
      </div>
    </li>
  </div>
</ul>

the desired output is 1 that match the third li with the text "test3"


回答1:

You can combine :not() and :has()

$(".jSC57 li:not(:has(button))")

Working demo

console.log($(".jSC57 li:not(:has(button))").find("p").text())
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="jSC57">
  <div class="PZuss">
    <li>
      <div>
        <p>test1</p>
      </div>
      <div>
        <button class="_0mzm- sqdOP  L3NKy   _8A5w5" type="button">Demandé</button>
      </div>
    </li>
    <li>
      <div>
        <p>test2</p>
      </div>
      <div>
        <button class="_0mzm- sqdOP  L3NKy   _8A5w5" type="button">Demandé</button>
      </div>
    </li>
    <li>
      <div>
        <p>test3</p>
      </div>
      <div>
      </div>
    </li>
    <li>
      <div>
        <p>test4</p>
      </div>
      <div>
        <button class="_0mzm- sqdOP  L3NKy   _8A5w5" type="button">Demandé</button>
      </div>
    </li>
  </div>
</ul>


回答2:

.not()Remove elements from the set of matched elements.

var item=$("li").not(":has(button)").length;
alert('li not containing button '+item);

https://jsfiddle.net/shoesheill/4dvmafre/10/


回答3:

$("ul[class="jSC57"] li:not(:has(button))");

回答4:

$(".jSC57 li:not(:has(button))").length;
  • 发表于 2018-12-30 11:49
  • 阅读 ( 297 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除