您好,我有大页面内容,但我想要指定 id
的整个页面我体内的内容。
问题:内body
标签<table id="quotation">
和jscode
不应该是removed
REST 所有内容都应从 body
中删除
这是我的页面:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The HTML5 Herald</title>
<meta name="description" content="The HTML5 Herald">
<meta name="author" content="SitePoint">
<link rel="stylesheet" href="css/styles.css?v=1.0">
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<!-- all other content to be removed -->
other content to be removed
<!-- all other content to be removed -->
<!-- should not be removed -->
<table id="quotation">
</table>
<script src="js/scripts.js"></script>
<!-- should not be removed -->
</body>
</html>
我尝试过这样的事情:
var html = $('html').html();
var body = $(html).find('body').not('#quotation').not('script').remove();
console.log($(html).find('body').html(body).html());
以上代码未定义
最佳答案
您可以使用 *
删除所有元素节点,并使用 :not()
选择器删除您想要的元素节点:
$('body *:not(#quotation):not(script)').remove();
然后文本节点使用:
$('body').contents().filter(function() {
return this.nodeType == 3;
}).remove();
//Remove ELEMENT nodes
$('body *:not(#quotation):not(script)').remove();
//Remove TEXT nodes
$('body').contents().filter(function() {
return this.nodeType == 3;
}).remove();
console.log($('body').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<p></p>
<p></p>
other content to be removed
<div>1111</div>
<p></p>
<span>AAAA</span>
<table id="quotation">
</table>
<script src="js/scripts.js"></script>
<p></p>
</body>
关于javascript - 如何通过从页面中删除一些内容来获得整个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48206077/