简体   繁体   English

如何检查HTML元素是否没有用JavaScript面对用户的数据(例如文本或图片)?

[英]How to check if an HTML element has no user facing data (e.g. text or pictures) in javascript?

I have a web application that removes certain elements from a webpage, so after it is done with the initial pass, I want to remove all divs that used to contain data that no longer do. 我有一个Web应用程序,可从网页中删除某些元素,因此在完成初始遍历后,我想删除所有用于包含不再包含的数据的div。 So for example, after the initial pass, an element might look like this: 因此,例如,在初始传递之后,一个元素可能看起来像这样:

<div class = "some-random-class">
    <script> *script stuff* </script>
    <div class = "some-other-class"> 
        <p>  </p>
    </div>
</div>

Where I want to be able to detect that the div with "some-random-class" no longer contains any text (or the text is only whitespace), pictures, nor video, and only contains elements that are used for formatting or spacing. 我希望能够检测到“ some-random-class”的div不再包含任何文本(或者该文本仅是空白),图片或视频,并且仅包含用于格式化或间隔的元素。 For example, this link shows two empty boxes in a webpage made up with only divs and scripts. 例如,此链接显示仅由div和脚本组成的网页中的两个空白框。

Here it is. 这里是。 If it has class name "some-other-class" and is empty then it will be deleted. 如果类名称为“ some-other-class”并且为空,则将其删除。 You can change var elements = document.getElementsByTagName('div'); 您可以更改var elements = document.getElementsByTagName('div'); to var elements = document.getElementsByTagName('*'); var elements = document.getElementsByTagName('*'); if you want all tags, or change it to any other tag. 如果您需要所有标签,或将其更改为任何其他标签。 Also you can remove elements[i].className == "some-other-class" && if you want to delete any element that is empty even without having that class name. 如果您想删除任何没有元素名称的空元素,也可以删除elements[i].className == "some-other-class" &&

 var elements = document.getElementsByTagName('div'); for (var i = 0; i < elements.length; i++) { if (elements[i].className == "some-other-class" && elements[i].innerText.replace(" ","") == "" && elements[i].innerHTML.indexOf('<img') < 0 && elements[i].innerHTML.indexOf('<video') < 0){ elements[i].parentElement.removeChild(elements[i]); } } 
 <div class = "some-random-class"> <div class = "some-other-class"> <p> a </p> </div> <div class = "some-other-class"> <p> <img src="https://www.w3schools.com/tags/smiley.gif"> </p> </div> <div class = "some-other-class"> <p> </p> </div> </div> 

暂无
暂无

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

相关问题 如何使用Javascript检查html中的父元素是否是特定的标记名(例如span) - How to check if parent element in html is specific tagname (e.g. span) using Javascript 如何检查2位小数? 例如2.2.10 - how to check 2 decimals? E.g. 2.2.10 检查用户是否是来自 Google Analytics 的回访者(例如) - Check if user is returning visitor (e.g.) from Google Analytics 查询任意 html 元素,例如查看它是否被禁用,使用shinyjs - query an arbitrary html element, e.g. to see if it is disabled, with shinyjs 如何创建文本并将文本追加到现有元素(例如,空跨度)? - How to createText and append text to an existing element e.g. empty span? 单击时如何使用Javascript(而非jQuery)获取元素的属性(例如标题)? - How to get the attribute (e.g., title) of an element when clicked on, with Javascript (not jQuery)? 如何将数组元素与innerHTML连接? 例如element [0] .innerHTML - How to connect array element with innerHTML? e.g. element[0].innerHTML 如何在特定的JavaScript代码中加载外部HTML页面? 例如,在JS函数中 - How to load an external HTML page within a specific JavaScript code? E.g. Within a JS function 如何检查浏览器是否缓存了特定资源(例如图像)? - How to check if a particular resource (e.g. image) is cached by the browser? 如何使SVG元素(例如矩形)scrollIntoView? - How to make a svg element (e.g. rectangle) scrollIntoView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM