简体   繁体   English

w3c验证程序忽略jQuery和javascript更改后的更改

[英]w3c validator ignores changes after jQuery & javascript changes

I have a website that needs to pass every error on W3C Validator. 我有一个网站,需要通过W3C验证程序上的每个错误。 A couple of the errors I have are related with the alt text of the images. 我遇到的几个错误与图片的替代文字有关。 I wrote this and added it on the <head> 我写了这个并将其添加到<head>

<script type="text/javascript">
        jQuery(document).ready(function($) {

            $('img').each(function(){
                var $img = $(this);
                var filename = $img.attr('src');
                $img.attr('alt', filename.substr(filename.lastIndexOf("/") + 1));
            });

        });
</script>

However, I saw that W3 validator ignores any change I do with javascript and took the HTML page just before the javascript runs. 但是,我看到W3验证程序忽略了我对javascript所做的任何更改,并在运行javascript之前获取了HTML页面。 A similar one is when I load a map with JS and the W3 validator sees it as an empty element. 类似的情况是,当我使用JS加载地图时,W3验证程序将其视为空元素。

Is there any way to make the validator waits before it checks the page? 有什么方法可以使验证器在检查页面之前等待?

The point of validating HTML is to ensure that it can be read properly by the browser and other tools (such as web crawlers) and conforms correctly to the HTML specification. 验证HTML的目的是确保浏览器和其他工具(例如Web爬网程序)可以正确读取HTML,并正确符合HTML规范。 By the time your code runs, that's already happened , and anything invalid that the browser handles has already been handled, and anything invalid the browser can't handle has already prevented the document from being interpreted properly. 在您的代码运行时, 这已经发生 ,并且浏览器处理的所有无效操作已经被处理,浏览器无法处理的任何无效操作已经阻止了文档的正确解释。 It's too late at that point to try to retroactively make the HTML valid; 现在要追溯使HTML有效是为时已晚。 it's already been read. 它已经被阅读。

Put it another way: Your img tags remain invalid, all you're doing is fixing the resulting img elements the browser created. 换句话说,您的img 标签仍然无效,您要做的就是修复浏览器创建的img 元素

The only way to make those img tags valid is to add the alt to them in the HTML file itself, with (as somethinghere points out ) useful content rather than simply the source filename. 使这些img 标签有效的唯一方法是在HTML文件本身中向其添加alt ,并使用有用的内容(如此处指出 ),而不是简单地使用源文件名。

You're editing the dom, the Validator does not check the DOM but only the HTML. 您正在编辑dom,验证程序不检查DOM,而仅检查HTML。 You need to add an alt attribute to every pictures yourself. 您需要自己为每张图片添加alt属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM