[英]Vanilla JS hide Div if contains a string
我正在處理有時在顯示產品時返回錯誤代碼的 CMS。
我目前正在學習基本的 JS,搜索了整個堆棧溢出,但找不到適用的答案。
在使用的產品部分,每個產品都會出現“#ResourceNotFound: ProjectResources, inside# New Construction : No”的錯誤代碼。
有沒有一種簡單的方法可以使用javascript從前端隱藏這些片段?
我曾經嘗試過的代碼是
document.querySelectorAll('.text-medium').forEach((element, index) => {
if (element.innerText.includes('New Construction : No')) {
element.style.display = 'none';
}
})
您需要檢查是否存在這兩個短語,因為它們位於單獨的<div>
元素中。 此外,使用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.