如何使用 cheerio 解包子元素?

How to unwrap child element using cheerio?

我正在使用 node.js。

我正在使用 cheerio html 解析器读取 html 文档。

在这个例子中

 <div class="a b c">
      <a id="ddd"/>
      sample
 </div>

我试过了

  var cheerio = require('cheerio');
  var c$ = cheerio.load(/*html content*/);
  var cntext = c$('div').html();

cntext 包含所需的 div 及其所有子项。

如何删除 div 并仅保留子节点?

提前致谢。

你需要 select div,依靠 selector 方法,以及节点的特定属性来区分所需的 div 和所有其他

$('div[class="a b c"]')

https://github.com/cheeriojs/cheerio#selectors

您需要直接迭代 selected div、

的 children

$('div[class="a b c"]').children().each(fn)

https://github.com/cheeriojs/cheerio#childrenselector

您需要在预先找到的 div

之后移动每个 children
$('div[class="a b c"]').children().each(function(index, element){
  $(element).insertAfter($(element).parent());
})

https://github.com/cheeriojs/cheerio#each-functionindex-element-

你需要删除空的div

$('div[class="a b c"]').remove()

https://github.com/cheeriojs/cheerio#remove-selector-

临时写的,让我知道。

使用jquery

的简单解决方案

You can simply convert it to html element using $(HTML_SSTRING_VAR) to create a html and use .html() function to get innerHTML like in the first case can use

var s=$( "#Div" ).html();
var temp=$(s);
alert(temp.html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div id="Div">
 
<div class="demo-container">
  <div class="demo-box">Demonstration Box</div>
</div>
  </div>
最简单的纯javascript解

Just find the substring between last occurrence of "<" and first occurrence of ">" which would be the content assuming that native javscript functions should work perfectly .

Javscript Substring Function Documentation

var s=$( "#Div" ).html();
s = s.substring(s.indexOf(">")+1,s.lastIndexOf("<")-1);
alert(s);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div id="Div">

<div class="demo-container">
  <div class="demo-box">Demonstration Box</div>
</div>
  </div>

另一种简单的方法

Just Create a div append the html content inside it and access the required content as innerHTML of the child div in the process we by pass the div which was to be eliminated

innerHTML Documentation

Children Documentation

var s=$( "#Div" ).html();
var temp = document.createElement('div');
temp.innerHTML = s;
alert(temp.children[0].innerHTML);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div id="Div">

<div class="demo-container">
  <div class="demo-box">Demonstration Box</div>
</div>
  </div>
 

.unwrap()Returns: jQuery Description: Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. .unwrap() documentation

ALL IN ONE FIDDLE