简体   繁体   English

“元素 img 上属性 src 的错误值:必须为非空”,对于动态生成的 img src

[英]"Bad value for attribute src on element img: Must be non-empty", for dynamically generated img src

I have a web site with an image slider. I keep the some of the image tags empty as the images load on when slide comes into view for faster page load.我有一个 web 站点,图像为 slider。我将一些图像标签留空,因为当幻灯片进入视图时加载图像以加快页面加载速度。 The image tags defined as follows:图片标签定义如下:

<img data-src="img/portfolio-desktop1-small.png" src="" alt=""/>

What I'm doing is on slide function I change the src to data-src with jQuery animation. The slider works great.我正在做的是在幻灯片 function 上,我将src更改为data-src源 jQuery animation。slider 效果很好。 My problem is when I try to validate it in w3c validation tool it gives the following error:我的问题是当我尝试在w3c 验证工具中验证它时出现以下错误:

Line 131, Column 179: Bad value for attribute src on element img : Must be non-empty.第 131 行,第 179 列:元素img上属性src的错误值:必须为非空。

 ...data-src="img/portfolio-desktop1-small.jpg" src="" alt=""/>

Syntax of URL: URL 的语法:
Any URL. For example: /hello , #canvas , or http://example.org/ .任何 URL。例如: /hello#canvashttp://example.org/ > Characters should be represented in NFC and spaces should be escaped as %20 . > 字符应在 NFC 中表示,空格应转义为%20

Is there anyway to fix this without altering the JavaScript or CSS?有没有办法在不改变 JavaScript 或 CSS 的情况下解决这个问题? If I leave it like this what can be the possible harmful outcomes of this matter?如果我就这样离开,这件事可能带来的有害后果是什么?

Set the image src attribute to # :将图像src属性设置为#

<img data-src="img/portfolio-desktop1-small.png" src="#" alt="Thumbnail">

The HTML passes the W3C validator just fine, and modern browsers know not to try to load the non-existent image.* HTML 通过 W3C 验证器就好了,现代浏览器知道不要尝试加载不存在的图像。*

For contrast, using a src value that references a non-existent file results in an unnecessary HTTP request and an error:相比之下,使用引用不存在文件的src值会导致不必要的 HTTP 请求和错误:

<img data-src="img/portfolio-desktop1-small.png" src="bogus.png" alt="Thumbnail">

Failed to load resource: The requested URL was not found on this server.无法加载资源:在此服务器上找不到请求的 URL。

*Note: I've read conflicting information on how browsers handle the # . *注意:我已经阅读了有关浏览器如何处理#相互矛盾的信息。 If anyone has definitive information, please add a comment.如果有人有确切的信息,请添加评论。

Also see related answer by sideshowbarker about the action attribute:另请参阅sideshowbarker关于action属性的相关回答:
https://stackoverflow.com/a/32491636 https://stackoverflow.com/a/32491636

Update: December 2019更新: 2019 年 12 月

It seems the src="#" trick used to be a decent workaround but not anymore.似乎src="#"技巧曾经是一个不错的解决方法,但现在不再是了。 What I do now is create a Gulp build task to post-process src="#" to use an inline data URL of a tiny invisible one pixel SVG.我现在要做的是创建一个 Gulp 构建任务来对src="#"进行后处理,以使用一个微小的不可见的单像素 SVG 的内联数据 URL

The essential gulpfile.js bits look like:基本的gulpfile.js位如下所示:

const onePixelSvg = '<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>';
const placeholderSvg = `"data:image/svg+xml;base64,${Buffer.from(onePixelSvg).toString('base64')}"`;

const task = {
   buildHtml() {
      return gulp.src('src/html/**/*.html')
         .pipe(replace('src="#"', 'src=' + placeholderSvg))
         .pipe(gulp.dest('build'));
      }
   };
gulp.task('build-html', task.buildHtml);

The advantage of using a build task is that 1) the source remains uncluttered, 2) the HTML always validates, and 3) the inline data URL prevents the browser from making an unnecessary and invalid network request.使用构建任务的优点是 1) 源保持整洁,2) HTML 始终验证,以及 3) 内联数据 URL 防止浏览器发出不必要和无效的网络请求。

20211 月更新。 src="#" 技巧现在适用于https://www.w3.org/TR/html-media-capture/的验证器

What happens if you just remove the src attribute then add it on the fly when you need it.如果您只是删除 src 属性,然后在需要时即时添加它,会发生什么情况。 The src attribute isn't required. src 属性不是必需的。 And in my opinion I wouldn't worry about what the w3c validation tool says anyway.在我看来,无论如何我都不会担心 w3c 验证工具会说什么。 As long as you test it in the necessary browsers and it works.只要你在必要的浏览器中测试它并且它工作。

If anyone still looking for the answer, the src="/" code resolves the w3c validator complains and doesn't produce additional request like the solution with the # character.如果有人仍在寻找答案, src="/"代码解决了 w3c 验证程序的抱怨,并且不会像带有#字符的解决方案那样产生额外的请求。

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

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