gpt4 book ai didi

javascript - HTML 标签未替换

转载 作者:行者123 更新时间:2023-12-01 03:05:18 24 4
gpt4 key购买 nike

我有一段 html,其中有一些 <br>其中的标签

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

我正在尝试替换 <br>标签为 <p>使用 JavaScript 的标签

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

问题是,这不起作用,也没有做任何该死的事情。我什至尝试在 CodePen 上测试它。

最佳答案

两个问题。 .replace() 返回函数的结果,并且不进行内联替换。作为docs状态,“此方法不会更改调用它的 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>

关于javascript - HTML 标签未替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46242585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com