gpt4 book ai didi

javascript - 无法使用 Jquery 更改 css 'background-image'

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:16 25 4
gpt4 key购买 nike

我正在尝试循环浏览一系列图片以在我的网页上制作照片查看器。循环方法工作正常,但将消息传输到 css 却不行。我想知道我的 javascript 代码中是否存在任何语法问题或我遗漏了一个概念。我知道骑自行车是有效的,因为我的警报有效。

var changeIt = ""
var backgroundPic = new Array(4);

backgroundPic[0] = '("images/displayPic1.jpg")';
backgroundPic[1] = '("images/displayPic2.jpg")';
backgroundPic[2] = '("images/displayPic3.jpg")';
backgroundPic[3] = '("images/displayPic4.jpg")';
backgroundPic[4] = '("images/displayPic5.jpg")';

var picCounter = 0;
var numberOfPics = 5;
var picTimer;

function setPic(){
alert("hi I want this pic: " + backgroundPic[picCounter]);
$('slider').css('background-image', url + "backgroundPic[picCounter]");

picCounter += 1;
if(picCounter >= numberOfPics){
picCounter = 0;
}

}

$(document).ready(function(){

$('.slider').css('background-image', backgroundPic[picCounter]);

picTimer = setInterval(setPic, 2000);

});

最佳答案

问题是由于您在连接 CSS 属性值时使用的语法不正确。试试这个:

var backgroundPic = [ 'images/displayPic1.jpg', 'images/displayPic2.jpg', 'images/displayPic3.jpg', 'images/displayPic4.jpg', 'images/displayPic5.jpg' ];    
var picCounter = 0;
var picTimer;

function setPic() {
// note the . in the jquery object below to indicate a class selector
$('.slider').css('background-image', 'url("' + backgroundPic[picCounter % backgroundPic.length] + '")');
picCounter++;
}

$(document).ready(function() {
picTimer = setInterval(setPic, 2000);
setPic();
});

关于javascript - 无法使用 Jquery 更改 css 'background-image',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37421767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com