jQuery 代码上的 W3 xHTML 验证错误!

发布于 2024-10-15 11:51:32 字数 1431 浏览 0 评论 0原文

我有一些 jQuery 代码,如果文档中没有它,它可以很好地通过验证,但如果包含它,则会导致错误。有问题的代码在这里:

    $.ajax({
        type: "GET",
        url: "data.xml",
        dataType: "xml",
        success: function(xml) {

            //Update error info
            errors = $(xml).find("Errors").find("*").filter(function ()
            {
                return $(this).children().length === 0;
            });

            if (errors.length == 0)
            {
                statuscontent = "<img src='/web/resources/graphics/accept.png' alt='' /> System OK";
            }
            else
            {
                statuscontent = "<img src='/web/resources/graphics/exclamation.png' alt='' /> "+errors.length+" System error"+(errors.length>1?"s":"");
            }

            $("#top-bar-systemstatus a").html(statuscontent);

            //Update timestamp
            $("#top-bar-timestamp").html($(xml).find("Timestamp").text());

            //Update storename
            $("#top-bar-storename").html("Store: "+$(xml).find("StoreName").text());
        }
    });

页面上有大量其他 jQuery 代码,它们都工作正常并且不会导致错误,所以我不太明白这有什么问题。该页面不是“实时”的,因此不幸的是无法提供指向该页面的链接。

它列出的错误是

文档类型不允许此处使用元素“img”

它指向的代码行在这里:

 statuscontent = "<img src='/web/resources/graphics/accept.png' alt='' /> System OK";

它还存在下一个分配给 statuscontent 的问题

I have some jQuery code that, without it in the document it passes validation fine, but with it in it causes errors. The code in question is here:

    $.ajax({
        type: "GET",
        url: "data.xml",
        dataType: "xml",
        success: function(xml) {

            //Update error info
            errors = $(xml).find("Errors").find("*").filter(function ()
            {
                return $(this).children().length === 0;
            });

            if (errors.length == 0)
            {
                statuscontent = "<img src='/web/resources/graphics/accept.png' alt='' /> System OK";
            }
            else
            {
                statuscontent = "<img src='/web/resources/graphics/exclamation.png' alt='' /> "+errors.length+" System error"+(errors.length>1?"s":"");
            }

            $("#top-bar-systemstatus a").html(statuscontent);

            //Update timestamp
            $("#top-bar-timestamp").html($(xml).find("Timestamp").text());

            //Update storename
            $("#top-bar-storename").html("Store: "+$(xml).find("StoreName").text());
        }
    });

There are loads of other jQuery code on the page which all works fine and causes no errors so I cannot quite understand what is wrong with this. The page isn't "live" so cannot provide a link to it unfortunately.

The error it lists is

document type does not allow element "img" here

And the line of code it points to is here:

 statuscontent = "<img src='/web/resources/graphics/accept.png' alt='' /> System OK";

It also has an issue with the next assignment to statuscontent

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

没有你我更好 2024-10-22 11:51:32

这是因为您使用的是 XHTML。这确实是无效的,因为验证器的行为就像脚本标记之间的代码是 XML(XHTML 是 XML 的子集)一样。这是无效的 XHTML,因为 img 标签不属于 script 标签内部。

要使其验证,请使用 CDATA 转义脚本中的任何 XML。

<script type="text/javascript">
// <![CDATA[

... your code
// ]]>
</script>

在 HTML 5 中这不是问题。如果您创建一个新网站,我建议您使用 HTML 5 而不是 XHTML。仅当您修改现有网站时才使用 XHTML。

It's because you are using XHTML. This is invalid indeed as the validator acts as if the code between the script tags is XML (XHTML is a subset of XML). This is invalid XHTML, as img-tags don't belong inside of script-tags.

To make it validate, use CDATA to escape any XML within your script.

<script type="text/javascript">
// <![CDATA[

... your code
// ]]>
</script>

In HTML 5 this is not an issue. If you make a new website I'd recommend HTML 5 over XHTML. Use XHTML only if you are modifying an existing website.

ペ泪落弦音 2024-10-22 11:51:32

请参阅与 HTML 5 的差异中的脚本和样式元素

接下来请参阅XHTML 媒体类型文档中的类似部分 因为您几乎可以肯定正在为您的服务提供服务XHTML 作为 IE 兼容性的 HTML。

(那么您可能会做我在 99 年所做的事情,即举起双手,尖叫“如果我必须假装它是 HTML,那么使用 XHTML 有何意义?!”然后跑回 HTML 4.01 (或者可能在 HTML 5 上))

See Script and Style Elements in The Differences From HTML 5.

Next see the comparable section in the XHTML Media Types document since you are almost certainly serving your XHTML as HTML for IE compatibility.

(Then you'll probably do what I did back in '99 which is to throw you hands up in the air, scream "What's the point of using XHTML if I have to pretend it is HTML?!" and run back to HTML 4.01. (Or possibly onto HTML 5))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文