如何从页面中删除一些内容来获取整个页面

时间:2018-01-11 11:33:40

标签: javascript jquery html

嗨我有很大的页面内容,但我希望整个页面内有指定的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());

以上代码未定义

1 个答案:

答案 0 :(得分:0)

您可以使用*删除所有元素节点,除了使用: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>