Extract element by ID from string?

后端 未结 2 1444
失恋的感觉
失恋的感觉 2021-02-20 05:59

I have a string which contains this text:




    ExtractDiv test



        
2条回答
  •  清酒与你
    2021-02-20 06:35

    You can parse an HTML string with the native DOMParser:

    var str = "

    Then just use regular DOM methods:

    console.log( doc.getElementById("main") )
    

    Note that using a DOMParser is more efficient and safer than inserting the string somewhere in your document's innerHTML, because only the structure will be created —

提交回复
热议问题