我有 Javascript 字串
"<div>"
"<h2>What color does the blackcurrant berry actually have?</h2>"
"<img src="img_girl.jpg" alt="Girl in a jacket" width="500" height="600">"
"<audio><source src="horse.ogg" type="audio/ogg"></audio>"
"</div>"
并且預期的結果是三個單獨的字串。像
expected output:
console.log(title) // "<h2>What color does the blackcurrant berry actually have?</h2>"
console.log(image) // "<img src="img_girl.jpg" alt="將 HTML 標記從 javascript 字串中分離成單獨的字串" width="500" height="600">"
請幫助如何做到這一點。提前致謝。
uj5u.com熱心網友回復:
我會將 JS 字串渲染成 HTML,然后訪問它的children.
document.getElementsByTagName("body")[0].innerHTML = "<div>"
"<h2>What color does the blackcurrant berry actually have?</h2>"
"<img src=`img_girl.jpg` alt='Girl in a jacket' width=`500` height=`600`>"
"<audio><source src=`horse.ogg` type='audio/ogg'></audio>"
"</div>"
let title = document.getElementsByTagName("body")[0].children[0].children[0]
let image = document.getElementsByTagName("body")[0].children[0].children[1]
console.log(title)
console.log(image)
<body></body>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/465296.html
標籤:javascript html
下一篇:為什么粘性標題卡住了?
