我的 JQuery 脚本有一个问题。
每次点击另一个 div 时,我都必须更改 Div(母)背景,但这些 DIV 有背景,所以我必须从 CHILDER 更改为 MOTHER div 背景。
这是我的脚本:
$(document).ready(function() {
$(".room-gallery .sub-images .sub-image").click(function(e) {
e.preventDefault();
var currentImage = $(".sub-image").css("background");
$(".main-picture").css("background", currentImage);
});
})
<!-- Gallery Start -->
<div class="room-gallery">
<div class="col-md-12 clear">
<div class="main-picture" style="background: url(images/gallery/img-1.jpg) center center no-repeat;">
<a href="#main-picture"></a>
</div>
</div>
<div class="col-md-4 sub-images clear">
<div class="sub-image" style="background: url(images/gallery/img-2.jpg) center center no-repeat;"></div>
</div>
<div class="col-md-4 sub-images clear">
<div class="sub-image" style="background: url(images/gallery/img-3.jpg) center center no-repeat;"></div>
</div>
<div class="col-md-4 sub-images clear">
<div class="sub-image" style="background: url(images/gallery/img-4.jpg) center center no-repeat;"></div>
</div>
</div>
<!-- Gallery End -->
所以我必须在每次点击 .sub-images 时将 DIV 背景更改为 .main-picture。
我不知道我做错了什么:(
你能帮帮我吗? 谢谢!
最佳答案
我相信你想要的是:
var currentImage = $(this).css("background");
代替:
var currentImage = $(".sub-image").css("background");
因为现在它在 DOM 中选择第一个类为 .sub-image
的 div
关于jquery - 从另一个 DIV 每次点击更改 DIV BG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33475675/