Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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没有';无法设置背景图像_Jquery - Fatal编程技术网

jquery没有';无法设置背景图像

jquery没有';无法设置背景图像,jquery,Jquery,我有这个html代码 <div class="gallery-item"><img src="images/thumbnails/img_(1).jpg" /></div> 这是Jquery代码 $(document).ready(function(e) { $(".gallery-item").each(function(index, element) { var img = $(this).children("img").attr("src

我有这个html代码

<div class="gallery-item"><img src="images/thumbnails/img_(1).jpg" /></div>
这是Jquery代码

$(document).ready(function(e) {
  $(".gallery-item").each(function(index, element) {
     var img = $(this).children("img").attr("src");
     $(this).css("background-image", "url("+img+")");
     $(this).children("img").remove();
  });
});
但它不起作用。只需删除img标签并离开。画廊项目没有任何背景! 但当我尝试这段代码时,它是有效的:

$(this).css("background-image", "url()");
只需将我的索引页地址设置为背景图像

我还试着设置背景色,效果很好


帮帮我吧

试试
backgroundImage
。将
console.log(img)
添加到为该变量赋值的行后,看看它是否真的包含您期望的内容。@putvande它也不起作用@CBroe:我试过警惕(img),一切都很好!但还是没用!试试这个
$(this.css(“背景图像”,“url('../“+img+”))”)背景图像值的格式为“url(地址)”,但src值只是一个纯地址!我测试过了!它也不起作用:(
  $(this).css("background-image", 'url("'+img+'")');
  $(this).css("background-image", 'url("'+img+'")');