当您使用 javascript 填充需要子元素的元素时,如何编写有效的 XHTML 1.0 Strict 代码?

发布于 2024-07-07 11:10:44 字数 1048 浏览 12 评论 0原文

我正在通过 W3C 的验证器运行我的网站,试图使其按照 XHTML 1.0 Strict 进行验证,但我遇到了一个特别棘手的(至少根据我的经验)验证错误。 我在网站中包含了来自各种服务的某些徽章,这些徽章提供了自己的 API 和代码以包含在外部网站上。 这些徽章(大部分)使用 javascript 来填充您在需要子标记的标记中插入的元素。 这意味着最终会生成完全有效的标记,但对于验证器来说,它看到的只是一个不完整的父子标记,然后会抛出错误。

需要注意的是,我知道我可以向服务机构投诉他们的徽章无法验证。 如果没有这个,我假设有人在包含这样的徽章的同时验证了他们的代码,这就是我感兴趣的。诸如“向 Flickr 投诉他们的徽章”之类的答案不会对我有太大帮助。

额外的警告:我希望标记尽可能保持语义。 IE 添加空的 li 标签或 tr-td 对来使其验证将是一个不可取的解决方案,即使它可能是必要的。 如果这是验证的唯一方法,那么好吧,但请将答案倾向于语义标记。

举个例子:

<div id="twitter_div">
<h2><a href="http://twitter.com/stopsineman">@Twitter</a></h2>
<ul id="twitter_update_list">
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/stopsineman.json?callback=twitterCallback2&amp;count=1"></script>
</ul>
</div>

注意 ul 标签包裹着 javascript。 最终通过脚本用 lis 填充,但对于验证器来说,它只能看到未填充的 ul。

提前致谢!

I'm running my site through the W3C's validator trying to get it to validate as XHTML 1.0 Strict and I've gotten down to a particularly sticky (at least in my experience) validation error. I'm including certain badges from various services in the site that provide their own API and code for inclusion on an external site. These badges use javascript (for the most part) to fill an element that you insert in the markup which requires a child. This means that in the end, perfectly valid markup is generated, but to the validator, all it sees is an incomplete parent-child tag which it then throws an error on.

As a caveat, I understand that I could complain to the services that their badges don't validate. Sans this, I assume that someone has validated their code while including badges like this, and that's what I'm interested in. Answers such as, 'Complain to Flickr about their badge' aren't going to help me much.

An additional caveat: I would prefer that as much as possible the markup remains semantic. I.E. Adding an empty li tag or tr-td pair to make it validate would be an undesirable solution, even though it may be necessary. If that's the only way it can be made to validate, oh well, but please lean answers towards semantic markup.

As an example:

<div id="twitter_div">
<h2><a href="http://twitter.com/stopsineman">@Twitter</a></h2>
<ul id="twitter_update_list">
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/stopsineman.json?callback=twitterCallback2&count=1"></script>
</ul>
</div>

Notice the ul tags wrapping the javascript. This eventually gets filled in with lis via the script, but to the validator it only sees the unpopulated ul.

Thanks in advance!

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

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

发布评论

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

评论(6

月寒剑心 2024-07-14 11:10:44

以下片段是有效的 XHTML 并执行此操作:

<div id="twitter_div">
    <h2 class="twitter-title"><a href="http://twitter.com/stopsineman" title="Tim's Twitter Page.">Twitter Updates</a></h2>
    <div id="myDiv" />
</div> 

<script type="text/javascript">
    var placeHolderNode = document.getElementById("myDiv");
    var parentNode = placeHolderNode.parentNode;
    var insertedNode = document.createElement("ul");
    insertedNode .setAttribute("id", "twitter_update_list");
    parentNode.insertBefore( insertedNode, placeHolderNode);
    parentNode.remove(placeHolderNode);
</script>
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/stopsineman.json?callback=twitterCallback2&count=5"></script>

The following fragment is valid XHTML and does the job:

<div id="twitter_div">
    <h2 class="twitter-title"><a href="http://twitter.com/stopsineman" title="Tim's Twitter Page.">Twitter Updates</a></h2>
    <div id="myDiv" />
</div> 

<script type="text/javascript">
    var placeHolderNode = document.getElementById("myDiv");
    var parentNode = placeHolderNode.parentNode;
    var insertedNode = document.createElement("ul");
    insertedNode .setAttribute("id", "twitter_update_list");
    parentNode.insertBefore( insertedNode, placeHolderNode);
    parentNode.remove(placeHolderNode);
</script>
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/stopsineman.json?callback=twitterCallback2&count=5"></script>
眼藏柔 2024-07-14 11:10:44

也许您可以使用 javascript 编写初始徽章 HTML? 如果可以使用 javascript 填充徽章代码,您可能只想将其插入文档中,对吗?

您只需要确保您的文档编写发生在各种徽章的 javascript 之前。

您能否给出包含无效代码的页面的 HTML/链接的具体示例?

Perhaps you could use javascript to write the initial badge HTML? You'd probably only want the badge code to be inserted in your document if javascript were available to populate it, right?

You'd just need to make sure your document writing happens before the javascript for your various badges.

Could you give a specific example of the HTML / link to a page with the invalid code?

毁梦 2024-07-14 11:10:44

每个徽章的解决方案可能有所不同。 对于 Twitter 来说,您可以编写自己的回调函数。 以下是基于其徽章代码的示例:

<div id="twitter_div">
  <h2><a href="http://twitter.com/stopsineman">@Twitter</a></h2>
  <div id="twitter_update_list"></div>
</div>

<script type="text/javascript">
function updateTwitterCallback(obj)
{
  var twitters = obj;
  var statusHTML = "";
  var username = "";
  for (var i = 0; i < twitters.length; i++)
  {
    username = twitters[i].user.screen_name;
    statusHTML += ('<li><span>' + twitters[i].text + '</span> <a style="font-size:85%" href="http://twitter.com/' + username + '/statuses/' + twitters[i].id + '">' + relative_time(twitters[i].created_at) + '</a></li>');
  }
  document.getElementById('twitter_update_list').innerHTML = '<ul>' + statusHTML + '</ul>';
}
</script>
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/stopsineman.json?callback=updateTwitterCallback&count=1"></script>

The solutions might be different for each badge. In Twitter's case, you can just write your own callback function. Here's an example based on their badge code:

<div id="twitter_div">
  <h2><a href="http://twitter.com/stopsineman">@Twitter</a></h2>
  <div id="twitter_update_list"></div>
</div>

<script type="text/javascript">
function updateTwitterCallback(obj)
{
  var twitters = obj;
  var statusHTML = "";
  var username = "";
  for (var i = 0; i < twitters.length; i++)
  {
    username = twitters[i].user.screen_name;
    statusHTML += ('<li><span>' + twitters[i].text + '</span> <a style="font-size:85%" href="http://twitter.com/' + username + '/statuses/' + twitters[i].id + '">' + relative_time(twitters[i].created_at) + '</a></li>');
  }
  document.getElementById('twitter_update_list').innerHTML = '<ul>' + statusHTML + '</ul>';
}
</script>
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/stopsineman.json?callback=updateTwitterCallback&count=1"></script>
孤芳又自赏 2024-07-14 11:10:44

我放了一个

    • 中带有“display:none” 标签:
  • <ul id="twitter_update_list"><li style="display:none;">A</li></ul>
    
    <script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
    <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/01241.json?callback=twitterCallback2&count=1"></script>
    

    这不会干扰脚本,在这种情况下它可以工作,
    我认为这不是一个“不可取的解决方案”:)

    I put a <li> with "display:none" in the <ul> Tag:

    <ul id="twitter_update_list"><li style="display:none;">A</li></ul>
    
    <script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
    <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/01241.json?callback=twitterCallback2&count=1"></script>
    

    This does not disturb the script and in this case it works,
    and I think its not a "undesirable solution" :)

    霊感 2024-07-14 11:10:44

    在某个时刻该页面会变得有效,对吧? 这是唯一一次可以真正验证这一点。

    我不确定如果一个不平凡的页面是用大量 DOM 脚本构建的,那么它在构建过程中的每个点都会保持有效。

    At some point the page becomes valid, right? That's the only time it can really be validated.

    I'm not sure a non-trivial page will remain valid at every point during its construction if it's constructed with a lot of DOM scripting.

    十秒萌定你 2024-07-14 11:10:44

    这可能不是该主题上最流行的观点,但是...

    不要担心 100% 验证。 这没什么大不了的。

    验证的目的是使您的标记尽可能标准。 为什么? 因为被赋予不符合规范的标记(例如,未验证的标记)的浏览器会进行自己的错误检查以纠正错误并按照您希望用户看到的方式显示页面。 浏览器错误检查的质量各不相同,yadda-yadda-yadda,最好有有效的标记...但导致验证失败的甚至不是您的代码! 编写这些徽章的人可能在多个浏览器中测试了它们(当然,您也应该这样做),如果它们按预期工作,则将其保留。

    简而言之,验证没有奖励:)

    This might not be the most popular opinion on this topic, but...

    Don't worry about 100% validation. It's just not that big of a deal.

    The point of validation is to make your markup as standard as possible. Why? Because browsers that are given markup that doesn't conform to the spec (eg, markup that does not validate) do their own error checking to correct it and display the page the way you intended it to look to the user. The quality of the browsers error checking varies, yadda-yadda-yadda, it's better to have valid markup... But it's not even your code that's causing the validation to fail! The people who wrote those badges probably tested them in multiple browsers (and you should do the same, of course), if they work as expected then just leave it at that.

    In short, there's no prize for validating :)

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