簡體   English   中英

如何從 html 字符串中獲取值?

[英]How to get Value from html string?

我有一個像這樣的 html 字符串,它來自 API 的響應:

const response = `
<html>
  <head>
    <title></title>
  </head>
  <body>
     <code>Value I want to get<br></code>
  </body>
</html>
`;

所以內容可能是動態的,但標簽<code>是唯一的。 我想對此有更多的解決方案,看看什么是最好的解決方案。

您可以使用DOMParser將您的 HTML 字符串轉換為 Document 對象,然后您可以使用querySelector()來獲取所需的值:

 const response = ` <html> <head> <title></title> </head> <body> <code>Value I want to get<br></code> </body> </html> `; const {body} = new DOMParser().parseFromString(response, 'text/html'); const value = body.querySelector('code').innerText; // find <code> tag and get text console.log(value);

暫無
暫無

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

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