XHTML 和 & 的编码(与号) [英] Encoding of XHTML and & (ampersand)

查看:22
本文介绍了XHTML 和 & 的编码(与号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站符合 XHTML Transitional 除了一件事:&URL 中的 (&) 按原样书写,而不是 &

My website is XHTML Transitional compliant except for one thing: the & (ampersand) in the URL are written as it is, instead of &

也就是说,我页面中的所有网址通常都是这样的:

That is, all the URLs in my pages are usually like this:

<a href="http://www.example.org/page.aspx?x=1&y=2">Foo</a>

但是 XHTML 验证器会产生这个错误:

But XHTML validator generates this error:

无法为一般实体y"生成系统标识符

cannot generate system identifier for general entity "y"

... 它希望 URL 写成这样:

... and it wants the URL to be written like this:

<a href="http://www.example.org/page.aspx?x=1&amp;y=2">Foo</a>

问题在于 Internet Explorer 和 Firefox 没有正确处理 URL 并忽略 y 参数.如何使此链接正常工作并正确验证?

The problem is that Internet Explorer and Firefox don't handle the URL correctly and ignore the y parameter. How can I make this link work and validate correctly?

在我看来,如果浏览器不使用严格编码的 XHTML URL,就不可能编写 XHTML 页面.

It seems to me that it is impossible to write XHTML pages if the browsers don't work with strict encoded XHTML URLs.

你想看看实际情况吗?查看这两个链接之间的区别(按原样复制并粘贴它们):

Do you want to see in action? See the difference between these two links (copy and paste them as they are):

http://stackoverflow.com/search?q=ff&sort=newest

http://stackoverflow.com/search?q=ff&amp;sort=newest

推荐答案

我刚刚试过这个.你试图做的是正确的.在 HTML 中,如果您正在编写链接,& 字符应编码为 &amp; 您只能将 & 编码为 %26 如果您希望参数值包含与号.我刚刚写了一个包含链接的简单 HTML 页面:<a href="Default2.aspx?param1=63&amp;param2=hel">点击我</a>并且它工作正常:default2.aspx 收到了预期的参数并且源通过了验证.

I have just tried this. What you attempted to do is correct. In HTML if you are writing a link the & characters should be encoded as &amp; You would only encode the & as %26 if you wanted a parameter value to contain an ampersand. I just wrote a simple HTML page that contained a link: <a href="Default2.aspx?param1=63&amp;param2=hel">Click me</a> and it worked fine: default2.aspx received the parameters intended and the source passed validation.

& 的编码为 &amp; 在 HTML 中是必需的,而不是在链接中.当浏览器在 HTML 源代码中看到链接的 &amp; 时,它会将其解释为 & 符号,并且链接目标将符合预期.如果您将 URL 粘贴到浏览器地址栏中,它不会期望它是 HTML 并且不会尝试解释它可能包含的任何 HTML 编码.这就是您建议我们应该复制/粘贴到浏览器中的示例链接不起作用的原因,以及我们不希望它们起作用的原因.

The encoding of & as &amp; is required in HTML, not in the link. When the browser sees the &amp; in the HTML source for a link it will interpret it as an ampersand and the link target will be as intended. If you paste a URL into your browser address bar it does not expect it to be HTML and does not try to interpret any HTML encoding that it may contain. This is why your example links that you suggest we should copy/paste into a browser don't work and why we wouldn't expect them to work.

如果您发布更多实际代码,我们可能会看到您做错了什么,但通过在锚点中使用 &amp;,您似乎朝着正确的方向前进标签.

If you post a bit more of your actual code we might be able to see what you have done wrong, but you appear to be heading the right direction by using &amp; in your anchor tags.

这篇关于XHTML 和 &amp; 的编码(与号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆