html 将 < > 替换为 lt gt(文本)

html replace < > to lt gt (text)

我会将 < 替换为 <,将 > 替换为 >。

我在文本区域中有一个文本。我有一个提交按钮。当我点击时。

我在文本区域中获取了文本。我会将 < 替换为 < 并将 > 替换为 >.

我需要你的帮助。

不是:我的英语不好。

真的很简单。仅一 google 距离。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

function myFunction() {
  var textareaElm = document.getElementsByTagName("textarea")[0];
  var textareaValue = textareaElm.value;
  textareaValue = textareaValue.replace(/</g, '&lt');
  textareaValue = textareaValue.replace(/>/g, '&gt');
  console.log(textareaValue);
}
<p>Click the button to trigger a function that will output Result in console.</p>
<textarea></textarea>
<button onclick="myFunction()">Click me</button>