返回数据中的 javascript 与号 (&) 将不会显示为值
我有这段代码:
...
var aData = request.responseXML.getElementsByTagName('data')[0];
var sDescription = aData.getElementsByTagName('description')[0].firstChild.data;
alert(escape(sDescription));
document.getElementById('tempLabourLineDescription').value = sDescription;
...
sDescription 正在输出: SUPPORT ASSY-FUEL TANK MOUNTING, R&R (LH) (L-ENG)
我认为很明显我想在这里做什么(获取sDescription
到名为 tempLabourLineDescription
的字段中,但这不起作用
但是,如果我在我的 php 脚本中替换或删除该字符串中的 &-char ,则一切正常。好吧,所以我想,只是逃避该死的字符串。 在我删除 & 字符之前,警告字符串也不起作用。
这是在做什么?当 sDescription 从 xml 文件中出来时,它不是一个字符串吗? 我该如何解决这个问题?
I have this bit of code:
...
var aData = request.responseXML.getElementsByTagName('data')[0];
var sDescription = aData.getElementsByTagName('description')[0].firstChild.data;
alert(escape(sDescription));
document.getElementById('tempLabourLineDescription').value = sDescription;
...
sDescription is outputting: SUPPORT ASSY-FUEL TANK MOUNTING, R&R (LH) (L-ENG)
I think it is obvious what i want to do here (get the sDescription
in to a field called tempLabourLineDescription
but that just will not work.
However, if i in my php script replace or delete the &-char from that string it all works fine. So i thought, just escape the darn string. But that will just not work.
alerting the string doesn't work either until i remove the &-character.
What is doing this? Is sDescription not a string when it comes out of the xml file?
How can i solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
答案就在这个片段中:
您需要的是 XML。一个&其本身并不是合法的 XML。您需要像这样输出结果:
The answer is in this snippet:
You're expecting XML. An & by itself is not legal XML. You need to output your result like this:
如果不查看输出脚本,很难判断,但首先要尝试的是屏蔽 & 符号:
&
不过,更简洁的方法是将 CDATA 添加到 XML 输出中:
客户端的 XML 解析器应该可以理解它,没有问题。
It's very difficult to tell without seeing your output script, but the first thing to try is to mask the ampersand:
&
The neater way, though, would be to add CDATA to your XML output:
your XML parser on client side should understand it no problem.
您可以使用 HTML eqv 来转义 & 符号。
&
You escape the ampersand by using the HTML eqv.
&
如果您无法更改服务器的 XML 输出(这不是您的应用程序或其他问题),则“黑客”修复将是:
If you are unable to alter the XML output from the server (it's not your app or some other issue), a "hack" fix would be: