返回数据中的 javascript 与号 (&) 将不会显示为值

发布于 2024-08-19 09:35:57 字数 670 浏览 4 评论 0原文

我有这段代码:

...
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧故 2024-08-26 09:35:57

答案就在这个片段中:

var aData = request.responseXML...

您需要的是 XML。一个&其本身并不是合法的 XML。您需要像这样输出结果:

支撑总成-燃油箱安装,R&R (LH) (L-ENG)

The answer is in this snippet:

var aData = request.responseXML...

You're expecting XML. An & by itself is not legal XML. You need to output your result like this:

SUPPORT ASSY-FUEL TANK MOUNTING, R&R (LH) (L-ENG)

够运 2024-08-26 09:35:57

如果不查看输出脚本,很难判断,但首先要尝试的是屏蔽 & 符号: &

不过,更简洁的方法是将 CDATA 添加到 XML 输出中:

<data><![CDATA[SUPPORT ASSY-FUEL TANK MOUNTING, R&R (LH) (L-ENG)]]></data>

客户端的 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:

<data><![CDATA[SUPPORT ASSY-FUEL TANK MOUNTING, R&R (LH) (L-ENG)]]></data>

your XML parser on client side should understand it no problem.

尬尬 2024-08-26 09:35:57

您可以使用 HTML eqv 来转义 & 符号。 &

You escape the ampersand by using the HTML eqv. &

别在捏我脸啦 2024-08-26 09:35:57

如果您无法更改服务器的 XML 输出(这不是您的应用程序或其他问题),则“黑客”修复将是:

function htmlizeAmps(s){
  return s.replace(/\x26/g,"&"); //globalreplace "&" (hex 26) with "&"
}
document.getElementById('tempLabourLineDescription').value = htmlizeAmps(sDescription);

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:

function htmlizeAmps(s){
  return s.replace(/\x26/g,"&"); //globalreplace "&" (hex 26) with "&"
}
document.getElementById('tempLabourLineDescription').value = htmlizeAmps(sDescription);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文