Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript/jQuery更改第二个背景图像url_Javascript_Jquery_Css - Fatal编程技术网

如何使用JavaScript/jQuery更改第二个背景图像url

如何使用JavaScript/jQuery更改第二个背景图像url,javascript,jquery,css,Javascript,Jquery,Css,我有一个元素具有以下样式: .element { background: transparent url(backgroundimage1.png) no-repeat, transparent url(backgroundimage2.png) no-repeat; } 是否可以在不使用大量代码的情况下使用JavaScript或jQuery只更改第二个后台url?是的,可以使用jQuery进行少量字符串操作 var first_background = $(".ele

我有一个元素具有以下样式:

.element {
    background: transparent url(backgroundimage1.png) no-repeat,
        transparent url(backgroundimage2.png) no-repeat;
}

是否可以在不使用大量代码的情况下使用JavaScript或jQuery只更改第二个后台url?

是的,可以使用jQuery进行少量字符串操作

var first_background = $(".element").css('background').split(',')[0];
var second_background = $(".element").css('background').split(',')[1];
var new_background = //add new background here.
$(".element").css('background', first_background + ',' + new_background);

你就完成了。

试试这个:
$('.element').css('background','transparent url(backgroundimage1.png)不重复,transparent url(newimage.png)不重复'@sfandler我希望这个答案对你有用,如果正确,请把它标记为正确:D.@Zoheiry哦,是的,谢谢。。我投了赞成票,但不知何故忘了接受答案哈哈。@sfandler谢谢,随时可以问我任何问题;)