改变img src属性值在循环- jquery
Change img src attribute value in loop - jquery
我有一个carousel图像,我想src属性值被替换为其他数据-src属性值取决于窗口宽度。
<div id="mainCarousel">
<img class="img-carousel" src="img/carousel-main-img2.jpg" data-src="img/carousel-main-datasrc-1.jpg" data-srctwo="img/carousel-main-datasrctwo-1.jpg">
<img class="img-carousel" src="img/carousel-main-img3.jpg" data-src="img/carousel-main-datasrc-2.jpg" data-srctwo="img/carousel-main-datasrctwo-2.jpg">
function carouselImages() {
// set window width in a variable
var winWidth = $(window).width();
// set img DOM element in a variable
var imgCarousel = $('#mainCarousel .img-carousel');
// declare empty variable for img 'data-src' attribute
var dataSrc = $();
// declare empty variable for img 'data-srcTwo' attribute
var dataSrcTwo = $();
//set loop which will iterate on each img DOM element
for(var i=0; i<imgCarousel.length; i++) {
// set first width range condition
if(winWidth > 400 && winWidth < 768) {
// if width matches condition, store the value of each DOM element's 'data-src' attribute in previously declared variable
dataSrc = imgCarousel[i].attr('data-src');
// replace DOM element's 'src' attribute's value with 'data-src' attribute's value
imgCarousel[i].attr('src', dataSrc);
}
// set second width range condition
else if (winWidth >= 768) {
// if width matches condition, store the value of each DOM element's 'data-src' attribute in previously declared variable
dataSrcTwo = imgCarousel[i].attr('data-srcTwo');
// replace DOM element's 'src' attribute's value with 'data-src' attribute's value
imgCarousel[i].attr('src', dataSrcTwo);
}
}
};
$(document).ready(function(){
carouselImages();
});
$(window).resize(function () {
carouselImages();
});
我想要实现的是,当窗口宽度在401px和767px之间时,图像的src
值被data-src
值替换,当窗口宽度为>= 768px
时,data-srctwo
值被替换。
在构造函数时,我试图尽可能地合乎逻辑。但这行不通。无论是缩小屏幕和刷新,还是直接调整浏览器窗口的大小,src属性值都不会改变。另外,我得到以下错误消息
imgCarousel[我]。Attr不是函数
谁能告诉我我的函数背后的逻辑到底出了什么问题?为什么我得到错误信息,因为我认为你不一定需要在条件中有一个函数
var imgCarousel = $('#mainCarousel .img-carousel');
是一个匹配元素的数组。它只是一个DOM元素而不是一个jquery对象。所以当它循环时&如果应用attr
,则会抛出错误。
您需要将其转换为jquery对象,然后再对其应用.attr
方法
你可以通过以下方式之一来引用当前元素
$(imgCarousel[i]).attr('data-src');
在使用$.attr()
函数之前,必须将DOM元素引用强制转换为JQuery对象,方法如下:
function carouselImages() {
// set window width in a variable
var winWidth = $(window).width();
// set img DOM element in a variable
var imgCarousel = $('#mainCarousel .img-carousel');
// declare empty variable for img 'data-src' attribute
var dataSrc = $();
// declare empty variable for img 'data-srcTwo' attribute
var dataSrcTwo = $();
//set loop which will iterate on each img DOM element
for(var i=0; i<imgCarousel.length; i++) {
// set first width range condition
if(winWidth > 400 && winWidth < 768) {
// if width matches condition, store the value of each DOM element's 'data-src' attribute in previously declared variable
dataSrc = $(imgCarousel[i]).attr('data-src');
// replace DOM element's 'src' attribute's value with 'data-src' attribute's value
$(imgCarousel[i]).attr('src', dataSrc);
}
// set second width range condition
else if (winWidth >= 768) {
// if width matches condition, store the value of each DOM element's 'data-src' attribute in previously declared variable
dataSrcTwo = $(imgCarousel[i]).attr('data-srcTwo');
// replace DOM element's 'src' attribute's value with 'data-src' attribute's value
$(imgCarousel[i]).attr('src', dataSrcTwo);
}
}
};
$(document).ready(function(){
carouselImages();
});
$(window).resize(function () {
carouselImages();
});
你正在使用jquery函数,所以不需要for loop
.使用each
将帮助你更容易,并通过$(this)
设置src与当前元素
imgCarousel.each(function(){
if(winWidth > 400 && winWidth < 768) {
data-src = $(this).attr("data-src");
$(this).attr('src',data-src);
}
// set second width range condition
else if (winWidth >= 768) {
data-srcTwo = $(this).attr("data-srcTwo");
$(this).attr('src',data-srcTwo);
}
});
使用$(imgCarousel[i])
代替imgCarousel[i]
。imgCarousel[i]
是一个标签,$(imgCarousel[i])
是一个jquery对象
相关文章:
- 在 for 循环 jQuery 中使用 one()
- 如何使用 for 循环 jquery 将相应的值附加到相应的 dom 元素
- 如何在循环jQuery中的数组中存储数据
- 循环JQuery数组
- 将值添加到相同的变量循环jQuery中
- 使用For each循环-jquery创建变量
- 在循环 jQuery 上添加元素
- 需要访问每个循环 jQuery 之外的变量
- Javascript:将循环jQuery动画放在js函数中
- 简单的循环 JQuery
- 使用日期循环 jquery 隐藏元素
- 从循环 jQuery 命名变量
- 循环jquery $.get,如何知道循环何时完成
- 需要循环 jquery
- 循环jQuery中的多个executeSql
- 编写函数来循环jquery样式库中的元素
- 从我从循环(JQuery)中收集的表中删除行
- 在for循环Jquery/Javascript中设置延迟
- 循环JQuery对象
- For循环Jquery正在创建表