簡體   English   中英

jQuery在解析html時添加了額外的標簽

[英]jQuery adds extra tags on parsing html

我有一個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。

<p> <div>是無效的HTML。

http://www.w3.org/TR/html401/struct/text.html#h-9.3.1

看來DOM正在嘗試通過打開/關閉無效的<p>標簽來為您修復它。

根據HTML規范<p>只能包含沒有<div> Phrasing元素

jQuery(或實際上,瀏覽器)為您做的是將HTML打破為有效的HTML。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM