通过W3C验证获取&符号

时间:2015-03-11 12:21:38

标签: escaping w3c-validation ampersand

我的网站菜单结构包含查询字符串,显然包含&符号。完整菜单HTML是

 <div id="menu">
            <ul>
                <li><a href="~/Products.aspx?Gender=Male" class="inactive" runat="server">Men</a>
                    <ul>
                        <li><a href="~/Products.aspx?Gender=Male&amp;Type=Coats" runat="server">Coats</a></li>
                        <li><a href="~/Products.aspx?Gender=Male&amp;Type=Jeans" runat="server">Jeans</a></li>
                        <li><a href="~/Products.aspx?Gender=Male&amp;Type=Knitwear" runat="server">Knitwear</a></li>
                        <li><a href="~/Products.aspx?Gender=Male&amp;Type=Shoes" runat="server">Shoes</a></li>
                        <li><a href="~/Products.aspx?Gender=Male&amp;Type=T-Shirts" runat="server">T-Shirts</a></li>
                    </ul>
                </li>
                <li><a href="~/Products.aspx?Gender=Female" class="inactive" runat="server">Women</a>
                    <ul>
                        <li><a href="~/Products.aspx?Gender=Female&amp;Type=Coats" runat="server">Coats</a></li>
                        <li><a href="~/Products.aspx?Gender=Female&amp;Type=Jeans" runat="server">Jeans</a></li>
                        <li><a href="~/Products.aspx?Gender=Female&amp;Type=Knitwear" runat="server">Knitwear</a></li>
                        <li><a href="~/Products.aspx?Gender=Female&amp;Type=Shoes" runat="server">Shoes</a></li>
                        <li><a href="~/Products.aspx?Gender=Female&amp;Type=T-Shirts" runat="server">T-Shirts</a></li>
                    </ul>
                </li>
                <li><a href="~/Products.aspx?Gender=Kids" class="inactive" runat="server">Kids</a>
                    <ul>
                        <li><a href="~/Products.aspx?Gender=Kids&amp;Type=Coats" runat="server">Coats</a></li>
                        <li><a href="~/Products.aspx?Gender=Kids&amp;Type=Jeans" runat="server">Jeans</a></li>
                        <li><a href="~/Products.aspx?Gender=Kids&amp;Type=Knitwear" runat="server">Knitwear</a></li>
                        <li><a href="~/Products.aspx?Gender=Kids&amp;Type=Shoes" runat="server">Shoes</a></li>
                        <li><a href="~/Products.aspx?Gender=Kids&amp;Type=T-Shirts" runat="server">T-Shirts</a></li>
                    </ul>
                </li>
                <li><a href="~/Stores.aspx" runat="server">Stores</a></li>
                <li><a href="~/Contact.aspx" runat="server">Contact</a></li>
            </ul>

        </div>

正如您所看到的,我已经正确地逃过了&符号。但是,当我通过W3C验证器运行页面时,我得到15个错误,说...

  

&安培;没有开始角色参考。 (并且可能应该已经作为&amp; amp;扩展了。)

这15个错误适用于&amp;放大器;在我的HTML中。有什么建议?谢谢。

2 个答案:

答案 0 :(得分:0)

忽略runat属性(HTML中不允许),您的代码是有效的HTML5。

您已使用&正确转义了&amp;的网址。您引用的错误不是由您的问题中的代码引起的。

您可以使用the W3C validator检查的最小示例:

<!DOCTYPE html>
<html lang="en">
<head><title>Example</title></head>
<body>
  <a href="~/Products.aspx?Gender=Kids&amp;Type=Coats">Coats</a>
</body>
</html>

答案 1 :(得分:0)

使用ASP.NET超链接控件而不是标准HTML

解决
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Products.aspx?Gender=Male&amp;Type=Coats">Coats</asp:HyperLink></li>