如何在 JavaScript 中使用 unescape() 函数?
我有一个 JSP 页面,其中有 JavaScript 函数,单击链接时将调用该函数。现在,当值到达 JavaScript 函数时,撇号就会被编码。
示例:
Name#039;s
#之前有&,本来应该是:
Name's
我使用了unescape()
解码函数,但似乎没有任何作用。最后,我不得不删除这些字符并添加撇号。有谁知道这个问题的解决办法吗?是不是JSP不支持&
的编码?当我在此页面中写入相同的编码值时,它将符号更改为撇号,这就是我在代码中想要的。
I have a JSP page in which I have JavaScript function that will be called when a link is clicked. Now, when the value reaches the JavaScript function, the apostrophe is encoded.
Example:
Name#039;s
Before # there is &, which originally should be:
Name's
I have used the unescape()
decode function, but nothing seems to work. In the end, I had to delete the characters and add the apostrophe. Does anyone know a fix for this? Is it that JSP doesn't support encoding for &
? When I was writing the same encode value in this page, it changed the symbol to the apostrophe, which is what I wanted in my code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
内置 Javascript 函数,例如
unescape()
、decodeURIComponent()
与您正在处理的字符串无关,因为您要解码的是 HTML实体。Javascript 中没有可用的 HTML entites 解码器,但由于您使用的是浏览器,如果该字符串被认为是安全,您可以执行以下操作(例如,在 JQuery 中)
它基本上会插入字符串作为 HTML 到
元素,然后提取其中的文本。
编辑:我刚刚意识到您发布的 JSP 输出不是真正的 HTML 实体;要处理给定的示例,您应该使用以下代码,在每个
#1234;
之前添加&
并将其设为Ӓ
:Built-in Javascript function such as
unescape()
,decodeURIComponent()
has nothing to do with the string you are working on, because the one you are looking to decode are HTML entites.There are no HTML entites decoder available in Javascript, but since you are working with a browser, if the string is considered safe, you may do the following (in JQuery, for example)
It bascially insert the string as HTML to a
<p>
element and then extract the text within.Edit: I just realize the JSP output you posted is not real HTML entities; To process the example given you should use the following, add
&
before every#1234;
and make itӒ
: