javascript - 如何通过从页面中删除一些内容来获得整个页面

标签 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());

以上代码未定义

最佳答案

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

相关文章:

javascript - 首先自动添加 wmode =“opaque” 来嵌入 jQuery?

javascript - 我正在尝试根据更改选择器使用 on() 附加事件

javascript - 服务器响应 404。ajax 调用未到达 Controller

javascript - 如何编译 require 语句?

javascript - 不可编辑的 jQuery 自动完成

javascript - 当自动回发打开时如何使用 javascript 选择 asp.net RadioButton

html - CSS/HTML : Preventing string of non-alphanumeric characters from being split by word wrap?

javascript - 如何让jszip生成相同的缓冲区

html - JasperReports html 导出

javascript - 调整大小时的全屏背景视频宽高比