为生成的图像提供 ID/类

发布于 2024-09-08 21:33:21 字数 759 浏览 3 评论 0 原文

我有大约 10 张来自 flickr 的图片。目前,它们只是以图像形式出现,没有个人 ID 或班级名称。

所以我有:

<img src="images/01.jpg" width="300" height="273" />
<img src="images/01.jpg" width="300" height="273" />
<img src="images/01.jpg" width="300" height="273" />
<img src="images/01.jpg" width="300" height="273" />

……等等。我想做的是:

<img id="image1" src="images/01.jpg" width="300" height="273" />
<img id="image2" src="images/01.jpg" width="300" height="273" />
<img id="image3" src="images/01.jpg" width="300" height="273" />
<img id="image4" src="images/01.jpg" width="300" height="273" />

但是因为它们是通过 jQuery 引入的,而不是手动引入的,所以我不确定如何按顺序将这些 ID 添加到图像中。每个人都需要有不同的风格。

有什么想法吗?

I have 10 or so images coming in from flickr. At the moment they just come in as images with no individual ID or Class names.

So I have:

<img src="images/01.jpg" width="300" height="273" />
<img src="images/01.jpg" width="300" height="273" />
<img src="images/01.jpg" width="300" height="273" />
<img src="images/01.jpg" width="300" height="273" />

...and so on. What I want to do is have:

<img id="image1" src="images/01.jpg" width="300" height="273" />
<img id="image2" src="images/01.jpg" width="300" height="273" />
<img id="image3" src="images/01.jpg" width="300" height="273" />
<img id="image4" src="images/01.jpg" width="300" height="273" />

But because they are being brought in via jQuery, and not manually, I'm unsure how to add these ID's, in order, to the images. Each needs to be styled differently.

Any ideas?

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

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

发布评论

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

评论(3

傾城如夢未必闌珊 2024-09-15 21:33:21

使用ajax的回调函数...

如果你有这样的东西,

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function (data) {
    $.each(data.items, function (i, item) {
        $("<img/>").attr({"src":item.media.m, 'id': 'images' + i}).appendTo("#images");
    });
});

use the callback function of your ajax...

if you have something like this,

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function (data) {
    $.each(data.items, function (i, item) {
        $("<img/>").attr({"src":item.media.m, 'id': 'images' + i}).appendTo("#images");
    });
});
峩卟喜欢 2024-09-15 21:33:21

您可以使用:

jqueryEl.attr("id", "someId");

或:

jqueryEl.addClass("newClass")

其中 jQueryEl 是包装元素。或者,如果您有一组图像,您可能会发现 attr 的函数选项很有帮助:

jquerySet.attr("id", function(index)
                     {
                       return "image" + (index + 1);
                     });

这实际上类似于 attr 文档

You can use:

jqueryEl.attr("id", "someId");

or:

jqueryEl.addClass("newClass")

where jQueryEl is a wrapped element. Or, if you have a set of images, you might find attr's function option helpful:

jquerySet.attr("id", function(index)
                     {
                       return "image" + (index + 1);
                     });

This is actually similar to an example in the attr docs.

離人涙 2024-09-15 21:33:21

如果您想发布获取图像的代码,它可能可以更好地集成在那里。但是,您也可以在添加图像后添加此代码。让我们假设图像已添加到 id 为 flickr 的 div 中:

$("#flickr img").each(function (i, image){
  image.id = "image" + (i + 1);
});

就是这样!现在,每个图像将按顺序具有 image1image2 等作为 id

If you want to post the code that fetches the images, it probably could be better integrated there. However, you could also just add this code after the images have been added. Lets pretend the images are added to a div with the id of flickr:

$("#flickr img").each(function (i, image){
  image.id = "image" + (i + 1);
});

Thats it! Now each image will have image1, image2, etc as the id, in order.

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