动态替换<和>为&lt;和&gt;

huangapple go评论59阅读模式
英文:

Dynamically replace < and > with &lt; and &gt;

问题

我尝试创建一个简单的JS函数,动态替换<pre>元素中的&lt;&gt;&amp;lt;&amp;gt;。为什么它不起作用?

let pres = document.querySelectorAll('pre');
for (let pre of pres) {
  pre.textContent = pre.textContent.replace(/</g, '&amp;lt;').replace(/>/g, '&amp;gt;');
}

稍后编辑:我实际想要实现的是,当我打开一个网页时,<pre>元素应该以HTML标记的文字形式显示,即:

&lt;!-- this is how html works! --&gt;
foo &lt;strong&gt;bar&lt;/strong&gt;
英文:

I try to make a simple JS function to dynamically replace &lt; and &gt; inside pre elements with &amp;lt; and &amp;gt;. Why it doesn't work?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let pres = document.querySelectorAll(&#39;pre&#39;);
for (pre in pres) {
  pre.textContent = pre.textContent.replace(&#39;&lt;&#39;, &#39;&amp;lt;&#39;);
  pre.textContent = pre.textContent.replace(&#39;&gt;&#39;, &#39;&amp;gt;&#39;);
}

<!-- language: lang-html -->

&lt;pre&gt;
foo &lt;strong&gt;bar&lt;/strong&gt;
&lt;/pre&gt;

<!-- end snippet -->

Later edit: What I actually want to achieve is that when I open a webpage, pre elements should display HTML markup literally, that is:

&lt;!-- this is how html works! --&gt;
foo &lt;strong&gt;bar&lt;/strong&gt;

答案1

得分: 2

&lt; 是 HTML 中的特殊字符。它代表标签的开始,而不是文本中的大于号。

因此,你的 HTML 中在 textContent 中没有任何 &lt;,只存在于 innerHTML 中:

for (const pre of document.querySelectorAll('pre')) {
  pre.textContent = pre.innerHTML;
}
<pre>
foo <strong>bar</strong>
</pre>
英文:

&lt; is a special character in HTML. It represents the start of a tag, not a greater than sign in the text.

Consequently your HTML doesn’t have any &lt; in the textContent, only in the innerHTML:

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

for (const pre of document.querySelectorAll(&#39;pre&#39;)) {
  pre.textContent = pre.innerHTML;
}

<!-- language: lang-html -->

&lt;pre&gt;
foo &lt;strong&gt;bar&lt;/strong&gt;
&lt;/pre&gt;

<!-- end snippet -->

答案2

得分: 1

另一种方法是将 &lt;&gt; 替换为 &amp;lt;&amp;gt;,将其存储在 result 中,然后将其包装在 code 标签中,并将其分配回 pre 元素。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
let preEl = document.querySelector('pre');
let specialChars = {
  '&lt;': '&amp;lt;',
  '&gt;': '&amp;gt;'
}
let result = '<code>';
for (let chr of preEl.innerHTML) {
  if (specialChars[chr]) {
    result += specialChars[chr]
  } else {
    result += chr
  }
}
result += '</code>';
preEl.innerHTML = result

<!-- language: lang-html -->
<pre>
foo &lt;strong&gt;bar&lt;/strong&gt;
</pre>

<!-- end snippet -->
英文:

Another approach by replacing &lt; and &gt; with &amp;lt; and &amp;gt;, store it in result then wrapped in a code tag and assigned it back to the pre element.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let preEl = document.querySelector(&#39;pre&#39;);
let specialChars = {
  &#39;&lt;&#39;: &#39;&amp;lt;&#39;,
  &#39;&gt;&#39;: &#39;&amp;gt;&#39;
}
let result = &#39;&lt;code&gt;&#39;;
for (let chr of preEl.innerHTML) {
  if (specialChars[chr]) {
    result += specialChars[chr]
  } else {
    result += chr
  }
}
result += &#39;&lt;/code&gt;&#39;;
preEl.innerHTML = result

<!-- language: lang-html -->

&lt;pre&gt;
foo &lt;strong&gt;bar&lt;/strong&gt;
&lt;/pre&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 04:51:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定