繁体   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