我需要将>更改为%3E ..,如果>符号被另一个符号更改,则一切正常。我该如何纠正?
<p id="demo">this > and this > symbol should be replaced.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/>/g, "%3E");
document.getElementById("demo").innerHTML = res;
}
您必须在正则表达式中找到>
(大于)并用<
代替。
<p id="demo">this > and this > symbol should be replaced.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/\>\;/g, "\%3E");
document.getElementById("demo").innerHTML = res;
}
</script>
[如果
<div>, <span>, or <noembed>
节点的子文本节点包含字符(&),(),innerHTML返回这些字符作为HTML实体"&", "<" and ">"
分别。使用Node.textContent获取这些文本的原始副本节点的内容。