使用 jQuery 循环遍历数组中的 img src 属性

发布于 2024-12-17 22:40:53 字数 2037 浏览 0 评论 0原文

我正在构建一个响应式网站,在一个特定页面上,我试图让移动设备(宽度 480 或更小)不下载特定图像。我知道我已经很接近了,我会在代码之后告诉你原因。

HTML:

<div class="index-section">
  <p><img class="scale-with-grid" alt="" /></p>
  <p><a href="#">A Letter from Person 1&nbsp;<span class="triangle">&#9654;</span></a></p>
</div>

<div class="index-section">
  <p><img class="scale-with-grid" alt="" /></p>
  <p><a href="">A Letter from Person 2&nbsp;<span class="triangle">&#9654;</span></a></p>
</div>

<div class="index-section">
  <p><img class="scale-with-grid" alt="" /></p>
  <p><a href="">A Letter from Person 3&nbsp;<span class="triangle">&#9654;</span></a></p>
</div>

<div class="index-section">
  <p><img class="scale-with-grid" alt="" /></p>
  <p><a href="">A Letter from Person 4&nbsp;<span class="triangle">&#9654;</span></a></p>
</div>

它基本上是 4 个几乎相同的 div。这是 jQuery:

$(document).ready(function() {

var imgPath = '_/img/'

var $winWidth = $(window).width();

var $indexSection = $('.index-section p img');

if ( $winWidth <= 480 ) {
    $indexSection.attr('src','');
    $indexSection.css('display','none');
    } else {
        var imgSrc = [ "keith-index.jpg", "derek-video.jpg", "bar-chart.jpg", "honor-roll.jpg" ];

        jQuery.each(imgSrc, function(value){
            $indexSection.attr('src',imgPath + value);
        });
   }
});

我将其放在 head 中(在 jQuery 库之后),以便它在渲染 HTML 时添加 src。这适用于 iPhone(或宽度小于 480 像素的 Firefox)并且不会加载图像,所以这很棒。

问题是,在 480px 以上,当我尝试循环浏览图像时,它几乎可以工作,因为 Firefox 的控制台告诉我它无法加载“http://myurl.com/_/img/0.jpg” ,它告诉我它几乎正确连接,但是由于某种原因,它打印的是数组的索引,而不是值(我认为)。

我需要的是,如果浏览器宽度超过 480,则将 url 打印为每个图像的 src 属性(“myurl.com/_/img/keith-index.jpg”,以及其他三个在各自的 div 中),以便图像加载到桌面浏览器上。

感谢您的任何帮助。

I'm building a responsive site, and on one particular page, I'm trying to have mobile devices (width 480 or less) not download particular images. I know I'm pretty close and I'll tell you why after the code.

HTML:

<div class="index-section">
  <p><img class="scale-with-grid" alt="" /></p>
  <p><a href="#">A Letter from Person 1 <span class="triangle">▶</span></a></p>
</div>

<div class="index-section">
  <p><img class="scale-with-grid" alt="" /></p>
  <p><a href="">A Letter from Person 2 <span class="triangle">▶</span></a></p>
</div>

<div class="index-section">
  <p><img class="scale-with-grid" alt="" /></p>
  <p><a href="">A Letter from Person 3 <span class="triangle">▶</span></a></p>
</div>

<div class="index-section">
  <p><img class="scale-with-grid" alt="" /></p>
  <p><a href="">A Letter from Person 4 <span class="triangle">▶</span></a></p>
</div>

It's basically 4 pretty much identical div's. And here's the jQuery:

$(document).ready(function() {

var imgPath = '_/img/'

var $winWidth = $(window).width();

var $indexSection = $('.index-section p img');

if ( $winWidth <= 480 ) {
    $indexSection.attr('src','');
    $indexSection.css('display','none');
    } else {
        var imgSrc = [ "keith-index.jpg", "derek-video.jpg", "bar-chart.jpg", "honor-roll.jpg" ];

        jQuery.each(imgSrc, function(value){
            $indexSection.attr('src',imgPath + value);
        });
   }
});

I put this in the head (after jQuery library) so that it adds the src as its rendering the HTML. This works on an iPhone (or Firefox at less than 480px wide) and doesn't load the images, so that's great.

The problem is that, above 480px, when I'm trying to loop through the images, it's almost working because Firefox's console tells me that it can't load "http://myurl.com/_/img/0.jpg", which tells me that it's concatenating almost correctly, but that, for some reason, it's printing the index of the array, rather than the value (I think).

What I need is, if the browser is wider than 480, to print the urls as the src attribute for each image ( "myurl.com/_/img/keith-index.jpg", and the other three in their respective div) so that the images load on, say, a desktop browser.

Thanks for any help.

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

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

发布评论

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

评论(1

梦中楼上月下 2024-12-24 22:40:53

对于 jQuery.each,第一个回调参数是集合中的索引,第二个参数是值。

jQuery.each(imgSrc, function(index, value){
    $indexSection.attr('src',imgPath + value);
});

编辑:哎呀...我错过了您正在尝试做的事情的一个更根本的问题。

$indexSection 是一个 jQuery 对象,其中包含您正在使用的所有图像。当您调用 $indexSection.attr 时,您将同时设置所有四个图像的属性。您对每个 imgSrc 值执行此操作,因此所有四个图像的 src 属性最终都会设置为最后一个 imgSrc 值。

相反,您希望循环遍历 img 标记,并从每个图像的 imgSrc 访问正确的值。

$indexSection.each(function(index) {
    $(this).attr('src', imgPath + imgSrc[index]);
});

With jQuery.each, the first callback argument is the index in the collection, and the second argument is the value.

jQuery.each(imgSrc, function(index, value){
    $indexSection.attr('src',imgPath + value);
});

Edit: Whoops... I missed a more fundamental problem with what you're trying to do.

$indexSection is a jQuery object containing all the images you're working with. When you call $indexSection.attr, you're setting the attribute for all four images at once. You do this for each of the imgSrc values, so all four images' src attributes end up being set to the last imgSrc value.

Instead, you want to loop over the img tags, and access the correct value from imgSrc for each image.

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