jquery - 从另一个 DIV 每次点击更改 DIV BG

标签 jquery html css

我的 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/

相关文章:

jQuery append href

html - 字体系列在重新加载后立即显示问题

javascript - Ajax 之前的 JQuery .each 处理

javascript - 如何使用 for() 实现 .map()?

jquery - 无法使用 CSS 创建下拉菜单? li 都在彼此之上?

javascript - 按类别对 JSON 内容进行排序

c# - 在 Javascript 中设置隐藏的输入值,然后在代码隐藏中访问它

php - 当我将 .html 文件更改为 .php 时,CSS 文件未正确链接

特定文本的 CSS 选择器

jquery - 用于使用拖放对列表进行排序或排序的触摸界面