使用 jQuery 循环遍历数组中的 img src 属性
我正在构建一个响应式网站,在一个特定页面上,我试图让移动设备(宽度 480 或更小)不下载特定图像。我知道我已经很接近了,我会在代码之后告诉你原因。
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>
它基本上是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 jQuery.each,第一个回调参数是集合中的索引,第二个参数是值。
编辑:哎呀...我错过了您正在尝试做的事情的一个更根本的问题。
$indexSection
是一个 jQuery 对象,其中包含您正在使用的所有图像。当您调用$indexSection.attr
时,您将同时设置所有四个图像的属性。您对每个imgSrc
值执行此操作,因此所有四个图像的src
属性最终都会设置为最后一个imgSrc
值。相反,您希望循环遍历
img
标记,并从每个图像的imgSrc
访问正确的值。With
jQuery.each
, the first callback argument is the index in the collection, and the second argument is the 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 theimgSrc
values, so all four images'src
attributes end up being set to the lastimgSrc
value.Instead, you want to loop over the
img
tags, and access the correct value fromimgSrc
for each image.