javascript - onClick使用jquery显示当前div

标签 javascript jquery html

我正在这里处理图像库,我希望 onClick 图像当前图像显示在 .gallery-container 中,该图像工作正常,但问题是当前图像标题未显示,因为我在 jquery 中定位图像源,我尝试添加标题也却无法实现。谁能指出我正确的方向。

$(document).ready(function() {
  $('.gallery-column img').on('click', function() {
    var expandImg = document.getElementById("expandedImg");
    expandImg.src = this.src;
    expandImg.parentElement.style.display = "block";
  });
});
* {
  box-sizing: border-box;
}

.gallery-wrap {
  width: 50%;
  height: 1066px;
  margin: 0 auto;
  display: flex;
}

.gallery-row {
  width: 52%;
  max-height: 497px;
}

.gallery-column {
  position: relative;
  padding: 10px;
}

.img-caption {
  position: absolute;
  bottom: 10%;
}

.gallery-column img {
  width: 100%;
  opacity: 0.8;
  cursor: pointer;
}

.gallery-column img:hover {
  opacity: 1;
}

.gallery-container {
  position: relative;
  width: 90%;
  height: 500px;
}

#expandedImg {
  width: 100%;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gallery-wrap">

  <div class="gallery-container" style="display: block;">
    <img id="expandedImg" src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60">
    <div class="img-caption">
      <h3>Still more than 2 Millions+ people using</h3>
    </div>
  </div>

  <div class="gallery-row">
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>Still more than 2 Millions+ people using</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542646822891-0a8451fce513?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=181de57e88385f8f32f48aef4e2831f9&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>olor sit amet, consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542632890661-441e6f424098?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=60444a082924abcdc72901abaa800620&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>sit amet, consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542635521008-f80f4d69bad4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a6e7baa2e8b1e236090d116d35bf0855&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>nsectetur adipiscing elit. Donec</h3>
      </div>
    </div>

    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542567055-2c294d7201bd?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=9d4553e0dddb5dfc9e8c2851c24e4610&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>consectetur adipiscing elit. Donec</h3>
      </div>
    </div>


  </div>
</div>

最佳答案

只需添加以下代码即可实现图像点击功能。

var _caption = $(this).next("div").html();
$("#expandedImgCaption").html(_caption);

expandedImgCaption 是展开图像的标题 ID

代码片段:

$(document).ready(function() {
  $('.gallery-column img').on('click', function() {
    var expandImg = document.getElementById("expandedImg");
    expandImg.src = this.src;
    expandImg.parentElement.style.display = "block";
    var _caption = $(this).next("div").html();
    $("#expandedImgCaption").html(_caption);
  });
});
* {
  box-sizing: border-box;
}

.gallery-wrap {
  width: 50%;
  height: 1066px;
  margin: 0 auto;
  display: flex;
}

.gallery-row {
  width: 52%;
  max-height: 497px;
}

.gallery-column {
  position: relative;
  padding: 10px;
}

.img-caption {
  position: absolute;
  bottom: 10%;
}

.gallery-column img {
  width: 100%;
  opacity: 0.8;
  cursor: pointer;
}

.gallery-column img:hover {
  opacity: 1;
}

.gallery-container {
  position: relative;
  width: 90%;
  height: 500px;
}

#expandedImg {
  width: 100%;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gallery-wrap">

  <div class="gallery-container" style="display: block;">
    <img id="expandedImg" src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60">
    <div id="expandedImgCaption" class="img-caption">
      <h3>Still more than 2 Millions+ people using</h3>
    </div>
  </div>

  <div class="gallery-row">
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542648108-66d2937f4bcf?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5ebbcae6a8310fd61d8a9fc82b79c792&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>Still more than 2 Millions+ people using</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542646822891-0a8451fce513?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=181de57e88385f8f32f48aef4e2831f9&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>olor sit amet, consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542632890661-441e6f424098?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=60444a082924abcdc72901abaa800620&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>sit amet, consectetur adipiscing elit. Donec</h3>
      </div>
    </div>
    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542635521008-f80f4d69bad4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a6e7baa2e8b1e236090d116d35bf0855&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>nsectetur adipiscing elit. Donec</h3>
      </div>
    </div>

    <div class="gallery-column">
      <img src="https://images.unsplash.com/photo-1542567055-2c294d7201bd?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=9d4553e0dddb5dfc9e8c2851c24e4610&auto=format&fit=crop&w=500&q=60" alt="">
      <div class="img-caption">
        <h3>consectetur adipiscing elit. Donec</h3>
      </div>
    </div>


  </div>
</div>

关于javascript - onClick使用jquery显示当前div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53392276/

相关文章:

javascript - event 是一个全局变量,可以在回调链中的任何地方访问吗?

javascript - 为什么这个函数返回一个函数?

javascript - 如何在 Javascript 中创建街机样式名称输入?

php - 上传文件中的数据

javascript - 在组件之间传递 props | react

javascript - 在 JavaScript 中循环遍历对象中的对象数组

javascript - 绑定(bind)另一个对象的下拉列表

javascript - 从 2 个字段中获取值并在第三个字段中显示乘法

jquery - SimpleWeather jquery 插件

javascript - 如何从 img 标签上传图片?