在 dom 准备好后,从 获取所有 data-src 并将它们设置在 src 属性中

发布于 2025-01-01 09:08:54 字数 1204 浏览 3 评论 0原文

在一个页面中我有这个:

<figure><img src="" data-src="img1.png"></figure>
<figure><img src="" data-src="img2.png"></figure>
<figure><img src="" data-src="img3.png"></figure>
<figure><img src="" data-src="img4.png"></figure>

然后继续。

我正在尝试在不使用 jquery 插件的情况下进行异步加载,并使其尽可能简单。

所以我想,当dom准备好并且页面完全加载时,将data-src设置为src。

如果我这样做: console.log($('figure img').attr('data-src')) 我只得到第一张图像。所以它给了我结果: img1.png

那么我怎么说呢,当 dom 准备好所有数字时 >图像> data-src 将被设置为该 img 的 src。

所以从这个:

<figure><img src="" data-src="img1.png"></figure>
<figure><img src="" data-src="img2.png"></figure>
<figure><img src="" data-src="img3.png"></figure>
<figure><img src="" data-src="img4.png"></figure>

到这个:

<figure><img src="img1.png"></figure>
<figure><img src="img2.png"></figure>
<figure><img src="img3.png"></figure>
<figure><img src="img4.png"></figure>

In a page I have this:

<figure><img src="" data-src="img1.png"></figure>
<figure><img src="" data-src="img2.png"></figure>
<figure><img src="" data-src="img3.png"></figure>
<figure><img src="" data-src="img4.png"></figure>

and goes on.

I am trying to make an async load without using a jquery plugin and make it as simple as possible.

So I thought, when the dom is ready and the page is fully loaded, set the data-src to the src.

If I do this: console.log($('figure img').attr('data-src')) I get only the first image. so it gives me result: img1.png

So how can I say, when dom ready all the figure > img > data-src to be set as src for that img.

So from this:

<figure><img src="" data-src="img1.png"></figure>
<figure><img src="" data-src="img2.png"></figure>
<figure><img src="" data-src="img3.png"></figure>
<figure><img src="" data-src="img4.png"></figure>

to this:

<figure><img src="img1.png"></figure>
<figure><img src="img2.png"></figure>
<figure><img src="img3.png"></figure>
<figure><img src="img4.png"></figure>

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

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

发布评论

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

评论(2

芸娘子的小脾气 2025-01-08 09:08:54

从 1.4.3 左右版本开始,jQuery 已经直接理解“data-”属性。因此,只需这样做:

$('figure img').each(function() {
  this.src = $(this).data('src');
});

您必须使用 .each() 来分离最初选择的列表中每个元素的处理,以便您执行的操作可以单独使用该元素。

Since version 1.4.3 or so, jQuery has understood "data-" attributes directly. So just do this:

$('figure img').each(function() {
  this.src = $(this).data('src');
});

You have to use .each() in order to separate out the processing of each element in the initially-selected list so that the operation you perform can use that element by itself.

却一份温柔 2025-01-08 09:08:54
$('figure > img').prop('src',function() {
    return $(this).attr('data-src');
});

或者使用 getAttribute() 更快一点。

$('figure > img').prop('src',function() {
    return this.getAttribute('data-src');
});

如果您确实想删除 data-src,请将 .removeAttr('data-src') 链接到末尾。

$('figure > img').prop('src',function() {
    return this.getAttribute('data-src');
}).removeAttr('data-src');
$('figure > img').prop('src',function() {
    return $(this).attr('data-src');
});

Or a little quicker with getAttribute().

$('figure > img').prop('src',function() {
    return this.getAttribute('data-src');
});

If you really want to remove the data-src, then chain .removeAttr('data-src') to the end.

$('figure > img').prop('src',function() {
    return this.getAttribute('data-src');
}).removeAttr('data-src');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文