Parse an HTML string with JS

后端 未结 10 1265
逝去的感伤
逝去的感伤 2020-11-21 07:17

I searched for a solution but nothing was relevant, so here is my problem:

I want to parse a string which contains HTML text. I want to do it in JavaScript.

10条回答
  •  醉酒成梦
    2020-11-21 07:30

    The following function parseHTML will return either :

    • a Document when your file starts with a doctype.

    • a DocumentFragment when your file doesn't start with a doctype.


    The code :

    function parseHTML(markup) {
        if (markup.toLowerCase().trim().indexOf('

    How to use :

    var links = parseHTML('Link 1Link 2').getElementsByTagName('a');
    

提交回复
热议问题