jquery没有';无法设置背景图像
我有这个html代码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
<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+'")');