我有一个 html 字符串。因为我需要对其进行一些操作,所以我需要使用 jquery。但是我遇到了问题,jQuery 向字符串添加了额外的标签。我怎样才能避免这种情况。
var html = marked(input); //returns an html string
console.log(html); //prints: <p>test<div>Type</div></p>
var tree = $("<div>" + html + "</div>");
//generate html out of the jquery html
html = tree.html();
console.log(html); // now it is: <p>test</p><div>Type</div><p></p>
我已经使用 jQuery 2.0.3 以及 firefox 和 chrome 浏览器对其进行了测试。
最佳答案
A <div>
在 <p>
里面不是有效的 HTML。
http://www.w3.org/TR/html401/struct/text.html#h-9.3.1
看来 DOM 正试图通过打开/关闭无效 <p>
来为您修复它标签。
关于javascript - jQuery 在解析 html 时添加了额外的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19411788/