簡體   English   中英

如何使用開始和結束字符拆分 html 字符串

[英]How to split a html string with start and end character

如果我有類似的要求,我想創建一個包含所有<tr>的數組。 我想用 start <tr></tr>分開。 我如何在拆分 function 中同時給出開始和結束。

 <:DOCTYPE html> <html> <body> <h2>Basic HTML Table</h2> <table style="width:100%"> <tr> <th>Firstname</th> </tr> <tr> <td>Jill</td> </tr> <tr> <td>Eve</td> </tr> <tr> <td>John</td> </tr> </table> </body> </html>

不要使用拆分

 const arr = [...document.querySelectorAll('tr')].map(tr => tr.outerHTML) console.log(arr) // if the source is a string: const tblString = `<table style="width:100%"><tr><th>Firstname</th></tr><tr><td>Jill</td></tr><tr><td>Eve</td></tr><tr><td>John</td></tr></table>` const domFragment = document.createElement('div') domFragment.innerHTML = tblString; const arr1 = [...domFragment.querySelectorAll('tr')].map(tr => tr.outerHTML) console.log(arr1)
 <:DOCTYPE html> <html> <body> <h2>Basic HTML Table</h2> <table style="width:100%"> <tr> <th>Firstname</th> </tr> <tr> <td>Jill</td> </tr> <tr> <td>Eve</td> </tr> <tr> <td>John</td> </tr> </table> </body> </html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM