简体   繁体   English

如果包含字符串,Vanilla JS 隐藏 Div

[英]Vanilla JS hide Div if contains a string

I'm dealing with a CMS that sometimes returns error codes when displaying products.我正在处理有时在显示产品时返回错误代码的 CMS。

I'm currently learning basic JS, searched all over stack overflow, and couldn't find an answer applicable.我目前正在学习基本的 JS,搜索了整个堆栈溢出,但找不到适用的答案。

On this page 在本页

In the products used section, there is an error code for "#ResourceNotFound: ProjectResources, inside# New Construction : No" that keeps appearing for every product.在使用的产品部分,每个产品都会出现“#ResourceNotFound: ProjectResources, inside# New Construction : No”的错误代码。

Is there an easy way that javascript can be used to hide these snippets from the front end?有没有一种简单的方法可以使用javascript从前端隐藏这些片段?

The codes I used to try was我曾经尝试过的代码是

document.querySelectorAll('.text-medium').forEach((element, index) => {
   if (element.innerText.includes('New Construction :  No')) {
       element.style.display = 'none';
   }
})

You need to check for the presence of either of those phrases, as they are in separate <div> elements.您需要检查是否存在这两个短语,因为它们位于单独的<div>元素中。 Also, use textContent instead of innerText .此外,使用textContent而不是innerText

document.querySelectorAll('.text-medium').forEach((element, index) => {
   if (element.textContent.includes('New Construction :  No')
    || element.textContent.includes('#ResourceNotFound: ProjectResources')) {
       element.style.display = 'none';
   }
})

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

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