javascript 在javascript中取消转义和号字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6697471/
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
Unescaping ampersand characters in javascript
提问by Will Reese
I'm having trouble properly displaying values that contain escaped characters (i.e. apostrophes are stored as \'
and not '
and brackets are >
and <
rather than >
and <
).
我无法正确显示包含转义字符的值(即撇号存储为\'
and not '
,括号是>
and<
而不是>
and <
)。
Items stored in my database have the characters (' < >
) escaped to (\' < >
), respectively. When I try to dynamically add them to the page with JavaScript, they print out in the escaped form in Firefox, rather than returning to their normal values like in IE (<
is being printed to the HTML rather just <
).
存储在我的数据库中的项目分别将字符 ( ' < >
) 转义为 ( \' < >
)。当我尝试使用 JavaScript 将它们动态添加到页面时,它们在 Firefox 中以转义形式打印出来,而不是像在 IE 中那样返回到它们的正常值(<
正在打印到 HTML 而只是<
)。
<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
var str = ">";
$(document.body).html(str);
});
</script>
</head>
<body>
</body>
</html>
I know that if I simply do a replace, I can print correctly, but by doing so, I'm allowing the injection of HTML code, which is why I escaped the string in the first place.
我知道如果我只是简单地进行替换,我可以正确打印,但是通过这样做,我允许注入 HTML 代码,这就是我首先对字符串进行转义的原因。
ADDED:
添加:
Firstly, I apologize about the mistakes in my initial post. After closer examination, in the instances where I am using $().html(), the strings are printing correctly. The times where they aren't printing correctly are when I am using code like below.
首先,我对我最初帖子中的错误表示歉意。经过仔细检查,在我使用 $().html() 的情况下,字符串打印正确。当我使用如下代码时,它们无法正确打印。
var str = ">";
$('#inputField').val(str);
In this instance, the text ">" is shown, rather than ">". Is there something I can do to fix this?
在这种情况下,将显示文本“>”,而不是“>”。我能做些什么来解决这个问题吗?
回答by Mrchief
You need to decode them like this:
您需要像这样解码它们:
$('#myText').val($("<div/>").html(str).text());
Demo: http://jsfiddle.net/QUbmK/
演示:http: //jsfiddle.net/QUbmK/
You can move the decode part to function too and call that instead:
您也可以将解码部分移动到函数中并调用它:
function jDecode(str) {
return $("<div/>").html(str).text();
}
$('#myText').val(jDecode(str));
回答by Mark Kahn
That doesn't happen, at least not with jQuery. If you do, literally: $(document.body).html('<div>')
, you will get <div>
printed to the screen, not a div
tag. If you're doing something not listed in your question:
这不会发生,至少在 jQuery 中不会发生。如果你这样做,字面意思是: $(document.body).html('<div>')
,你会被<div>
打印到屏幕上,而不是div
标签。如果您正在做的事情未在您的问题中列出:
either use .text()
instead of .html()
or replace all &
with &
:
使用.text()
代替.html()
或全部替换&
为&
:
$(document.body).text(str);
$(document.body).html(str.replace(/&/g, '&'))
回答by jfriend00
First off, you can't run the code you have in your example. document.body is not ready for manipulation in the HEAD tag. You have to run that after the document has loaded. If I put your code in a safe place to run, it works fine as you can see here.
首先,您无法运行示例中的代码。document.body 尚未准备好在 HEAD 标签中进行操作。您必须在文档加载后运行它。如果我将您的代码放在安全的地方运行,它可以正常工作,如您所见。
So ... there must be more to your situation than the simple example you show here. You can see your simple example works fine here when the code is put in the right place:
所以......你的情况肯定比你在这里展示的简单例子更多。当代码放在正确的位置时,您可以看到您的简单示例在这里工作正常: