使用javascript从网页中删除不需要的html内容
Remove unwanted html contents from webpage using javascript
我想从我的网页主体中删除不需要的html元标签和内容,因为这些标签会在移动视图中引起问题。并且这些内容来自外部服务器。想要删除div和表标记之间的所有内容
<div class="my_body">
我的html内容
<table>
您可以使用:not()
选择器并选择除table
之外的元素,然后使用remove()
将其删除
$('div.ymail_body>:not(table)').remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="row ymail_body">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<!--[if gte mso 9]> <style data-immutable> table td { border-collapse: collapse; }
</style>
<![endif]-->
<style data-immutable="">
@media only screen and (min-device-width: 320px) and (max-device-width: 480px) {
.mobile-hide {
display: none!important
}
.mobile-wide {
float: none!important;
width: 100%!important
}
.mobile-block {
display: block!important;
width: 100%!important
}
.reply {
padding: 5px 0 0 15px!important
}
}
</style>
<table>
<tr>
<td>1</td>
</tr>
</table>
</div>
或者您可以使用prevAll()
来选择所有以前的元素
$('div.ymail_body table').prevAll().remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="row ymail_body">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<!--[if gte mso 9]> <style data-immutable> table td { border-collapse: collapse; }
</style>
<![endif]-->
<style data-immutable="">
@media only screen and (min-device-width: 320px) and (max-device-width: 480px) {
.mobile-hide {
display: none!important
}
.mobile-wide {
float: none!important;
width: 100%!important
}
.mobile-block {
display: block!important;
width: 100%!important
}
.reply {
padding: 5px 0 0 15px!important
}
}
</style>
<table>
<tr>
<td>1</td>
</tr>
</table>
</div>
使用纯JavaScript,您可以使用
querySelectorAll()
[].forEach.call(document.querySelectorAll('div.ymail_body>:not(table)'), function(ele) {
ele.parentElement.removeChild(ele);
// ele.remove();
// remove() doesn't work for IE 7 and below
});
<div class="row ymail_body">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<!--[if gte mso 9]> <style data-immutable> table td { border-collapse: collapse; }
</style>
<![endif]-->
<style data-immutable="">
@media only screen and (min-device-width: 320px) and (max-device-width: 480px) {
.mobile-hide {
display: none!important
}
.mobile-wide {
float: none!important;
width: 100%!important
}
.mobile-block {
display: block!important;
width: 100%!important
}
.reply {
padding: 5px 0 0 15px!important
}
}
</style>
<table>
<tr>
<td>1</td>
</tr>
</table>
</div>
您可以尝试:
jQuery(".ymail_body").remove();
相关文章:
- jQuery自动完成显示不需要的动态html标记
- AngularJs正在解码HTML字符(不需要)
- 不带HTML的光标位置需要转换为带HTML的位置
- 使用AJAX html()加载Django模板导致不需要的文本
- 当不需要从 html 中使用$scope应该使用
- 在 HTML 中使用 Javascript/jQuery 的 SELECT 中几乎不需要更正
- 不需要的JavaScript注入到HTML中
- PHP echo,在对象(需要)之前输出完整的 html 代码(不需要)
- HTML图像“onclick”在调用javascript函数时重定向到另一个页面(不需要)
- 如何用javascript编程编辑HTML格式的文本,而不需要手动处理标记和HTML
- 从插件生成的代码中更改不需要的HTML
- html表单上不需要的自动填充
- 如何使用Javascript删除html标记中不需要的空格
- 使用javascript从网页中删除不需要的html内容
- 使用正则表达式从数组中删除不需要的html
- Bower应该自动包含库js和css文件吗;不需要在index.html中列出
- 在HTML中旋转图像-不需要
- Jquery从自定义元素中获取HTML,不需要关闭标记
- 当不需要时,删除HTML背景图像
- 是否可以使用HTML/CSS进行简单的计算,但不需要JavaScript或后端