简体   繁体   English

使用具有特定文字的li的ul隐藏div

[英]Hide div with ul that has li with specific text

I'm trying to hide a DIV if it contains a ul with li with a specific text. 我正在尝试隐藏DIV(如果它包含带有li的ul和特定文本)。

The one catch I have is that it can't be jQuery, pure JavaScript. 我要抓住的一个问题是它不能是纯JavaScript的jQuery。

 <div class="uc-events"> <h3 style="text-align: center;">Upcoming Events</h3> <ul class="upcoming_events graphical_theme"> <li>No upcoming events are scheduled </li> </ul> </div> 

You can use https://www.w3schools.com/jsref/met_document_queryselector.asp . 您可以使用https://www.w3schools.com/jsref/met_document_queryselector.asp If you know how to write a CSS selector for jQuery, that's how you can use it without jQuery. 如果您知道如何为jQuery编写CSS选择器,那就可以在没有jQuery的情况下使用它。

$('div > ul > li') becomes document.querySelectorAll("div > ul > li"); $('div > ul > li')成为document.querySelectorAll("div > ul > li");

Of course: 当然:

  • I haven't selected the div , but the li . 我还没有选择div ,而是li
  • I didn't check that the piece of text was included in the li . 我没有检查那段文字是否包含在li

So how do we do that? 那么我们该怎么做呢? We can't do it with CSS selectors, so we're going to use plain JS. 我们无法使用CSS选择器来做到这一点,因此我们将使用普通的JS。 We'll filter only the li elements that contain the text we want, then we'll take their grand-parent node (which is the div ). 我们将仅过滤包含所需文本的li元素,然后采用其祖父母节点(即div )。

// Get the li's in an array
let lis = Array.from(document.querySelectorAll("div > ul > li"));

// only keep the ones with the proper text
lis = lis.filter(item => item.text.match(/The text that you want to match/));

// get the div's from the li's
let divs = lis.map(item => item.parentNode.parentNode);

// do whatever you want to these divs.
...

This approach is more resilient than calling .innerHTML directly on the div, because in that scenario you might end up selecting div's with the required text somewhere random in their innerHTML. 这种方法比直接在div上调用.innerHTML更具弹性,因为在这种情况下,您最终可能会在其innerHTML中随便选择带有所需文本的div。 For instance something like: 例如:

<div class="uc-events">
  <h3 style="text-align: center;">Upcoming Events</h3>
  <ul class="upcoming_events graphical_theme">
    <li>No upcoming events are scheduled </li>
  </ul>
</div>

<div class="uc-events">
  <h3 style="text-align: center;">Upcoming Events</h3>
  <ul class="upcoming_events graphical_theme">
    <!--<li>No upcoming events are scheduled </li>-->
    <li>Event 1: 11/10 at 08:00</li>
  </ul>
</div>

<div class="uc-events">
  <h3 style="text-align: center;">Upcoming Events</h3>
  <ul class="upcoming_events graphical_theme">
    <li>Event 2: 12/10 at 08:00</li>
  </ul>
  <span>After that, you'll find out that No upcoming events are scheduled.</span>
</div>

Here with the .innerHTML methods from the other answers, all 3 div's are going to be hidden, instead of just the top one. 在这里,使用其他答案中的.innerHTML方法,所有3个div都将被隐藏,而不仅仅是顶部的一个。

You could do something like this. 你可以做这样的事情。 Get all the divs and find the ones with your requisite text and hide them. 获取所有div并找到包含所需文本的div并将其隐藏。 Please let me know if you would like clarification regarding the below code. 如果您想对以下代码进行澄清,请告诉我。

 document.getElementById("hide").addEventListener("click", ()=>{ let divs = document.getElementsByTagName("div"); for(let i = 0;i<divs.length; i++){ if(divs[i].innerHTML.includes("No upcoming events are scheduled")){ divs[i].style.display = "none"; } } }); 
 <div class="uc-events"> <h3 style="text-align: center;">Upcoming Events</h3> <ul class="upcoming_events graphical_theme"> <li> No upcoming events are scheduled </li> </ul> </div> <button id="hide">hide divs</button> 

Here's a naive solution using getElementsByTagName in nested loops: 这是在嵌套循环中使用getElementsByTagName的幼稚解决方案:

 const target = "scheduled"; for (const d of document.getElementsByTagName("div")) { for (const ul of d.getElementsByTagName("ul")) { for (const l of ul.getElementsByTagName("li")) { if (l.innerText.includes(target)) { d.style.display = "none"; } } } } 
 <div class="uc-events"> <h3 style="text-align: center;">Upcoming Events</h3> <ul class="upcoming_events graphical_theme"> <li> No upcoming events are scheduled </li> </ul> </div> 

 var targetStr = "No upcoming events are scheduled"; var divs = document.getElementsByTagName("div"); for(var i = 0; i < divs.length; i++){ var divsChildren = divs[i].children; for(var j = 0; j < divsChildren.length; j++){ if(divsChildren[j].tagName == "UL"){ var ulChildren = divsChildren[j].children; for(var k = 0; k < ulChildren.length; k++){ if(ulChildren[k].tagName == "LI"){ if(ulChildren[k].textContent.trim() === targetStr){ divs[i].style.display = "none"; } } } } } } 
 <div id="num">first div</div> <div class="uc-events"> <h3 style="text-align: center;">Upcoming Events</h3> <ul class="upcoming_events graphical_theme"> <li> No upcoming events are scheduled </li> </ul> </div> <div id="num">third div</div> 

Check below snippet where I get all the div elements and check if their innerHTML contains given text. 检查下面的代码片段,其中我获得了所有div元素,并检查它们的innerHTML是否包含给定的文本。 If you also want to check ul and li are present just add additional conditionals. 如果您还想检查ul和li是否存在,只需添加其他条件。

 const divList = Array.prototype.slice.call(document.getElementsByTagName('DIV')); console.log(divList); divList.forEach((el) => { if (el.innerHTML.includes('No upcoming events are scheduled')) { el.style.display = 'none'; } }) 
 <div class="uc-events"> <h3 style="text-align: center;">Upcoming Events</h3> <ul class="upcoming_events graphical_theme"> <li>No upcoming events are scheduled </li> </ul> </div> <div class="uc-events"> <h3 style="text-align: center;">Upcoming Events</h3> <ul class="upcoming_events graphical_theme"> <li>No upcoming events are scheduled </li> </ul> </div> <div class="uc-events"> <h3 style="text-align: center;">Upcoming Events</h3> <ul class="upcoming_events graphical_theme"> <li>No upcoming events are schedu </li> </ul> </div> 

Ahh ok, i just edited the post, here is a simple way to hide ur LI if there is no text inside using javascript 嗯,好吧,我刚刚编辑了帖子,如果使用javascript里面没有文本,这是一种隐藏ur LI的简单方法

  $('.testclass:empty').text("No News!"); 
 <div class="testclass"></div> <div class="testclass">Div with news</div> <div class="testclass"></div> <div class="uc-events"> <h3 style="text-align: center;">Upcoming Events</h3> <ul class="testclass"> <li><div class="testclass">IF NEWS DISPLAY DIV!</div></li> </ul> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM