Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.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
Jquery 将背景URL放入数组中_Jquery - Fatal编程技术网

Jquery 将背景URL放入数组中

Jquery 将背景URL放入数组中,jquery,Jquery,我有一个有几个div的容器。每个div都有一个背景图像。我需要将每个图像的URL放入一个数组中 <div id="images"> <div style="background-image: url(79cbf113f.jpg)" class="thumb" title=""></div> <div style="background-image: url(234234234.jpg)" class="thumb" title="">

我有一个有几个div的容器。每个div都有一个背景图像。我需要将每个图像的URL放入一个数组中

<div id="images">
    <div style="background-image: url(79cbf113f.jpg)" class="thumb" title=""></div>
    <div style="background-image: url(234234234.jpg)" class="thumb" title=""></div>
</div>
<button>Click</button>

$(document).on("click", "button", function() {
    var img = $('#images .thumb').attr("src");
});

点击
$(文档)。在(“单击”,“按钮”,函数()上){
var img=$('#images.thumb').attr(“src”);
});

如何获取这些信息?

使用
map
get

$(文档)。在(“单击”,“按钮”,函数()上){
var images=$('#images.thumb').map(函数(){
返回此.style.backgroundImage;
}).get();
控制台日志(图像);
});


单击divs上方的
映射
,并使用
.css
获取相应的css属性。您还可以使用regex处理该属性

$(文档).on(“单击”、“.button”,函数()){
console.log(
$('#images.thumb').map(函数(){
返回$(this.css('background-image')。替换(/url\(“(.+)”\)/,“$1”)
})
.toArray()
);
});


收集图像
@trincot updatedCool。我只需要图像名,没有路径。就这么做吧?感谢您可以使用一些本机DOM代码
this.style.backgroundImage.replace(/url\(“(.+)”\)/,“$1”)