javascript 如何使用javascript在文本输入中获取html特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11518383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:29:33  来源:igfitidea点击:

How to use javascript to get html special characters in text input

javascripthtmlescapingspecial-charactershtmlspecialchars

提问by kli

I want to sent some html special character to text field and then later use javascript to get it back in its original format: like if I sent "&pi", it will shows "π" on the text input, and when I use javascript to get it back, I should get "&pi", but I only can get "π", not "&pi". Please help, my code is like the following:

我想将一些 html 特殊字符发送到文本字段,然后使用 javascript 将其恢复为原始格式:就像我发送“&pi”一样,它会在文本输入中显示“π”,当我使用 javascript 时拿回来,我应该得到“&pi”,但我只能得到“π”,而不是“&pi”。请帮忙,我的代码如下:

<script type="text/javascript"> 
function qpush(a) {
    document.getElementById('input').value += a;
}

function show(a) {
    alert(document.getElementById('input').value);
}
</script>

<input type="text" id="input" />
<input type="button" onclick="qpush('&pi;');" value="&pi;" />
<input type="button" onclick="show()" value="go" />

采纳答案by Lior

Depending on what you need the it for you could do one of 3 options:

根据您的需要,您可以执行以下 3 个选项之一:

  1. use the javascript escape and unescape functions:

    var escaped = escape('π') // escaped = "%u03C0" which unescape will turn back into π

  2. If you have jQuery available it's html method might do the trick:

    $('input').val().html()

  3. This is a hack but will work: Save a copy of the value the way you want it returned on a 'data-' attribute of the input element, this will validate in html5 and won't break in other doctypes.

    document.getElementById('input').setAttribute('data-originalValue', '&pi')

  1. 使用 javascript 转义和 unescape 函数:

    var escaped = escape('π') // escaped = "%u03C0" unescape 将变回 π

  2. 如果您有可用的 jQuery,它的 html 方法可能会起作用:

    $('输入').val().html()

  3. 这是一个 hack 但会起作用:以您希望它在输入元素的“data-”属性上返回的方式保存值的副本,这将在 html5 中验证并且不会在其他文档类型中中断。

    document.getElementById('input').setAttribute('data-originalValue', '&pi')

回答by Quentin

The valueproperty will give you text, not HTML.

value属性将为您提供文本,而不是 HTML。

You could try getAttribute('value')but since &pi;and πare identically equivalent in HTML, a browser is entirely free to return πfor that too.

您可以尝试,getAttribute('value')但由于&pi;π在 HTML 中完全相同,因此浏览器也可以完全免费返回π

Once the HTML has been converted into a DOM, there is no way to access the original HTML (short of downloading the HTML again with XHR and parsing the raw text of it yourself).

一旦将 HTML 转换为 DOM,就无法访​​问原始 HTML(除了使用 XHR 再次下载 HTML 并自己解析原始文本之外)。

If you want the data to be "&pi;"then the HTML for it needs to be &amp;pi;.

如果您希望数据是,"&pi;"那么它的 HTML 需要是&amp;pi;.

If you want that and the rendering on the button to be πthen you need:

如果你想要那个和按钮上的渲染,π那么你需要:

<button type="button" value="&amp;pi;">π</button>

(Keeping in mind the various bugs that old-IE has with the button element)

(请记住旧 IE 对按钮元素的各种错误)

That will, of course, display &pi;when you copy the value to the text input.

当然,&pi;当您将值复制到文本输入时会显示。

回答by Christoph

Use this to replace all occurrences of the character:

使用它来替换所有出现的字符:

document.getElementById('input').value.replace(/π/g,"&pi;");