문제

I am using cheerio to perfom some html manipulation on node js server .I have an html string like this

var htmlString =" <ol>
  <li>
  <p>item1</p>
 </li>
 <li>
  <p>item2</p>
</li>
<li>
  <p>item 3</p>
</li>
   <li>
   <p>item 4</p>
</li>
</ol>
 <p>First paragraph</p>
<p>second paragraph</p>
<p>Third paragraph</p>
"

 var $ = cheerio.load(htmlString);
var dummy = $("<div></div>")
var item = dummy.append($("*").slice(0,3).clone()).html();

The output returned is

 <ol>
  <li>
  <p>item1</p>
 </li>
 <li>
  <p>item2</p>
</li>
<li>
  <p>item 3</p>
</li>
   <li>
   <p>item 4</p>
</li>
</ol>

<li>item1</li>
<p>item1</p>

The output that I expect is the ordered list followed byparagraph1 followed by paragraph2

Am I doing something wrong or is this a bug in cheerio?

도움이 되었습니까?

해결책

After fiddling with the code for the entire day I finally got the solution. Apparently I was loading the html fragment incorrectly. This worked for me

var $ = cheerio.load();
var dummy = $("<div></div>")
var item = dummy.append($(htmlString).slice(0,3)).html();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top