Parse an HTML string with JS

后端 未结 10 1248
逝去的感伤
逝去的感伤 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 07:28

    If you're open to using jQuery, it has some nice facilities for creating detached DOM elements from strings of HTML. These can then be queried through the usual means, E.g.:

    var html = "titleTesttest01test02test03";
    var anchors = $('
    ').append(html).find('a').get();

    Edit - just saw @Florian's answer which is correct. This is basically exactly what he said, but with jQuery.

提交回复
热议问题