我对特殊字符(如& ;、ä等)及其相应的HTML编码文字(如&,ä等)有疑问.
I have a problem with special characters like &, ä, etc. and their corresponding HTML encoded writing like &, ä etc.
-
在输入字段的value标记中使用它们时,浏览器会自动对其进行解码,完全没有问题.
When they are used in the value tag of input fields, the browser decodes it automatically, no problem at all.
使用jQuerys val()函数时,浏览器没有机会正确评估内容
When using jQuerys val() function, the browser has no chance to evaluate the content correctly
$("button").click(function(){ $("#test").val("&"); });
有什么主意吗?
推荐答案您可以使用这个古老的实体解码技巧:
You can use this age-old entity decoding trick:
$("button").click(function(){ $("#test").val($("<div>").html("&").text()); });
正在运行的演示: jsfiddle/AndyE/XegGy/
它将创建一个临时的<div>元素,将其HTML设置为&,然后获取文本值,该文本值将为&.
It creates a temporary <div> element, sets its HTML to & and then gets the text value, which will be &.
更多推荐
使用val()时特殊字符的Javascript/jQuery编码
发布评论