Parse an HTML string with JS

后端 未结 10 1266
逝去的感伤
逝去的感伤 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:25

    Create a dummy DOM element and add the string to it. Then, you can manipulate it like any DOM element.

    var el = document.createElement( 'html' );
    el.innerHTML = "titleTesttest01test02test03";
    
    el.getElementsByTagName( 'a' ); // Live NodeList of your anchor elements
    

    Edit: adding a jQuery answer to please the fans!

    var el = $( '
    ' ); el.html("titleTesttest01test02test03"); $('a', el) // All the anchor elements

提交回复
热议问题