A JavaScript parser for DOM

后端 未结 5 1376
难免孤独
难免孤独 2020-12-16 04:30

We have a special requirement in a project where we have to parse a string of HTML (from an AJAX response) client side via JavaScript only. Thats right

5条回答
  •  清酒与你
    2020-12-16 05:09

    You can leverage the current document without appending any nodes to it.

    Try something like this:

    function toNode(html) {
        var doc = document.createElement('html');
        doc.innerHTML = html;
        return doc;
    }
    
    var node = toNode(' This is the old title. ');
    
    console.log(node);​
    

    http://jsfiddle.net/6SvqA/3/

提交回复
热议问题