簡體   English   中英

HTML標記未替換

[英]HTML tag not replacing

我有一段帶有一些<br>標記的html

<div id="reading-project">
<p>The next morning, as soon as the sun was up, they started on their way, 
and soon saw a beautiful green glow in the sky just before them."That must be the Emerald City," said Dorothy.<br>
As they walked on, the green glow became brighter and brighter, and it seemed that at last they were nearing the end of their travels. Yet it was afternoon before they came to the great wall that surrounded the City. 
It was high and thick and of a bright green color.<br>
In front of them, and at the end of the road of yellow brick, was a big gate, all studded with emeralds that glittered so in the sun that even the painted eyes of the Scarecrow were dazzled by their brilliancy.<br>
There was a bell beside the gate, and Dorothy pushed the button and heard a silvery tinkle sound within. 
Then the big gate swung slowly open, and they all passed through and found themselves in a high-arched room, the walls of which glistened with countless emeralds.<br>
Before them stood a little man about the same size as the Munchkins. He was clothed all in green, from his head to his feet, and even his skin was of a greenish tint.
</p>
</div>

我正在嘗試使用JavaScript將<br>標簽替換為<p>標簽

document.getElementById('reading-project').innerHTML.replace('<br>', '<p>')

問題是,這行不通,也沒有做該死的事情。 我什至嘗試在CodePen上對其進行測試。

兩個問題。 .replace()返回函數的結果,並且不進行內聯替換。 正如文檔所述 ,“此方法不會更改調用的String對象。它只是返回一個新字符串。” 其次,您的代碼僅更改一個實例,並且要更改它們,就需要使用基本的正則表達式:

document.getElementById('reading-project').innerHTML = document.getElementById('reading-project').innerHTML.replace(/<br>/g, '<p>')

 document.getElementById('reading-project').innerHTML = document.getElementById('reading-project').innerHTML.replace(/<br>/g, '<p>') 
 <div id="reading-project"> <p>The next morning, as soon as the sun was up, they started on their way, and soon saw a beautiful green glow in the sky just before them."That must be the Emerald City," said Dorothy.<br> As they walked on, the green glow became brighter and brighter, and it seemed that at last they were nearing the end of their travels. Yet it was afternoon before they came to the great wall that surrounded the City. It was high and thick and of a bright green color.<br> In front of them, and at the end of the road of yellow brick, was a big gate, all studded with emeralds that glittered so in the sun that even the painted eyes of the Scarecrow were dazzled by their brilliancy.<br> There was a bell beside the gate, and Dorothy pushed the button and heard a silvery tinkle sound within. Then the big gate swung slowly open, and they all passed through and found themselves in a high-arched room, the walls of which glistened with countless emeralds.<br> Before them stood a little man about the same size as the Munchkins. He was clothed all in green, from his head to his feet, and even his skin was of a greenish tint. </p> </div> 

暫無
暫無

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

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