根据特定文本删除各种 div

我有这个DOM,我想像过滤器一样根据h5标签中包含的文本删除li。


<div class="total">

  <ul>

   <li>

    <h5>this super heroe is super cool: Clark Kent</h5>

   </li>

  </ul>


  <ul>

   <li>

    <h5>I always wanted to be Batman</h5>

   </li>

  </ul>


  <ul>

   <li>

    <h5>Somedays I will be transform as Spiderman </h5>

   </li>

 </ul>


  <ul>

   <li>

    <h5>This women is incredible Catwoman</h5>

   </li>

  </ul>


   <li>

    <ul>

     <h5>The worst character is Joker</h5>

   </ul>

   </li>


   <ul>

    <li>

     <h5>Someone knows about Green Lantern </h5>

    </li>

   </ul>

</div>

我需要根据字符串将过滤器放入此数组中,该数组没有昏迷


let appDataTab = ["Clark Kent    Catwoman     Joker"]


我的目标是删除所有李,它的h5不满足“克拉克肯特”,“猫女”和“小丑”,因为你可以看到appDataTab内容这些字符串没有分离。


有只小跳蛙
浏览 121回答 2
2回答

MMTTMM

以下是从我端验证后添加其他代码后的更新代码。<!DOCTYPE html><html><head>&nbsp; &nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script></head><body>&nbsp; &nbsp; <div class="total">&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5>this super heroe is super cool: Clark Kent</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5>I always wanted to be Batman</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5>Somedays I will be transform as Spiderman </h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5>This women is incredible Catwoman</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5>The worst character is Joker</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5>Someone knows about Green Lantern </h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; $(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var prohibited = ['Clark Kent', 'Catwoman', 'Joker'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("li").each(function (index) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var wordFound = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < prohibited.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($(this).text().indexOf(prohibited[i]) > -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wordFound = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (wordFound == false) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).parent().remove();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; </script></body></html>

饮歌长啸

您应该首先使其成为常规的逗号分隔数组,然后查找该元素并删除包含该元素的数组。li逐个注释步骤:$(document).ready(function () {//Your arrayvar appDataTab = ["Clark Kent&nbsp; &nbsp; Catwoman&nbsp; &nbsp; &nbsp;Joker"];//Split with more than 1 space (2 space)var appDataSplit = appDataTab[0].split("&nbsp; ");//Remove empty elementsvar filtered = appDataSplit.filter(function (el) {&nbsp; return el != "";});//Trim elements from extra spacesvar cleanFiltered = [];for (i=0; i < filtered.length; i++){&nbsp; cleanFiltered.push(filtered[i].trim());}console.log(cleanFiltered);//look for any li containing the words in cleanFilter array&nbsp; $("li").each(function(){&nbsp; &nbsp; for (i=0; i < cleanFiltered.length;i++){&nbsp; &nbsp; &nbsp; if ($(this).text().indexOf(cleanFiltered[i]) > -1) {&nbsp; &nbsp; &nbsp; &nbsp; $(this).remove();&nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; });});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="total">&nbsp; <ul>&nbsp; &nbsp;<li>&nbsp; &nbsp; <h5>this super heroe is super cool: Clark Kent</h5>&nbsp; &nbsp;</li>&nbsp; </ul>&nbsp; <ul>&nbsp; &nbsp;<li>&nbsp; &nbsp; <h5>I always wanted to be Batman</h5>&nbsp; &nbsp;</li>&nbsp; </ul>&nbsp; <ul>&nbsp; &nbsp;<li>&nbsp; &nbsp; <h5>Somedays I will be transform as Spiderman </h5>&nbsp; &nbsp;</li>&nbsp;</ul>&nbsp; <ul>&nbsp; &nbsp;<li>&nbsp; &nbsp; <h5>This women is incredible Catwoman</h5>&nbsp; &nbsp;</li>&nbsp; </ul>&nbsp; &nbsp;<ul>&nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp;<h5>The worst character is Joker</h5>&nbsp; &nbsp;</li>&nbsp; &nbsp;</ul>&nbsp; &nbsp;<ul>&nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp;<h5>Someone knows about Green Lantern </h5>&nbsp; &nbsp; </li>&nbsp; &nbsp;</ul></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript