我正在此处使用图片库,我希望.gallery-container中的onClick锚标签当前图像显示不起作用。先前在图片img
点击上,我得到了输出.gallery-column img
。
$('.gallery-column img').on('click', function(){
var expandImg = document.getElementById('expandedImg');
expandImg.src = this.src;
expandImg.parentElement.style.display = "block";
var imageCaption = $(this).next("div").html();
$("#expandedImgCaption").html(imageCaption);
});
此代码运行正常。现在,我添加了锚,它无法正常工作。 我已经尝试过但无法实现。
有人可以建议我吗?
$(document).ready(function() {
var carouselImg = document.getElementsByClassName('carousel-img');
$('a.post-carousel').on('click', function() {
var expandImg = document.getElementById('expandedImg');
var carouselImg = document.getElementsByClassName('carousel-img');
expandImg.src = carouselImg.src;
expandImg.parentElement.style.display = "block";
var imageCaption = $(this).next("div").html();
$("#expandedImgCaption").html(imageCaption);
});
});
* {
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.0/jquery.min.js"></script>
<div class="gallery-wrap">
<div class="gallery-container" style="display: block;">
<a href="#" class="post-carousel">
<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" class="carousel-img">
<div class="img-caption">
<h3>Still more than 2 Millions+ people using</h3>
</div>
</a>
</div>
<div class="gallery-row">
<div class="gallery-column">
<a href="#" class="post-carousel">
<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="" class="carousel-img">
<div class="img-caption">
<h3>Still more than 2 Millions+ people using</h3>
</div>
</a>
</div>
<div class="gallery-column">
<a href="#" class="post-carousel">
<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="" class="carousel-img">
<div class="img-caption">
<h3>olor sit amet, consectetur adipiscing elit. Donec</h3>
</div>
</a>
</div>
<div class="gallery-column">
<a href="#" class="post-carousel">
<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="" class="carousel-img">
<div class="img-caption">
<h3>sit amet, consectetur adipiscing elit. Donec</h3>
</div>
</a>
</div>
<div class="gallery-column">
<a href="#" class="post-carousel">
<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="" class="carousel-img">
<div class="img-caption">
<h3>nsectetur adipiscing elit. Donec</h3>
</div>
</a>
</div>
</div>
</div>
答案 0 :(得分:1)
尝试使用 this ,如下所示:
$(document).ready(function() {
var carouselImg = document.getElementsByClassName('carousel-img');
$('a.post-carousel').on('click', function() {
var expandImg = document.getElementById('expandedImg');
expandImg.src = $(this).find('img').attr('src');
var imageCaption = $(this).find('.img-caption > h3').text();
$('.gallery-container').find('.img-caption > h3').text(imageCaption);
});
});
* {
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;">
<a href="#" class="post-carousel">
<img id="expandedImg" class="img-caption" 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" class="carousel-img">
<div class="img-caption">
<h3>Still more than 2 Millions+ people using</h3>
</div>
</a>
</div>
<div class="gallery-row">
<div class="gallery-column">
<a href="#" class="post-carousel">
<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="" class="carousel-img">
<div class="img-caption">
<h3>Still more than 2 Millions+ people using</h3>
</div>
</a>
</div>
<div class="gallery-column">
<a href="#" class="post-carousel">
<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="" class="carousel-img">
<div class="img-caption">
<h3>olor sit amet, consectetur adipiscing elit. Donec</h3>
</div>
</a>
</div>
<div class="gallery-column">
<a href="#" class="post-carousel">
<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="" class="carousel-img">
<div class="img-caption">
<h3>sit amet, consectetur adipiscing elit. Donec</h3>
</div>
</a>
</div>
<div class="gallery-column">
<a href="#" class="post-carousel">
<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="" class="carousel-img">
<div class="img-caption">
<h3>nsectetur adipiscing elit. Donec</h3>
</div>
</a>
</div>
</div>
</div>
答案 1 :(得分:0)
可以简单地使用this
关键字定位相关的.carousel-img
来实现。
注意1::您错过了将ID expandedImgCaption
附加到img-caption
的{{1}}上的事情:
expandedImg
注意2:,您不需要<div id="expandedImgCaption" class="img-caption">
,因为div位于锚点之内而不是其旁边:
next()
$("#expandedImgCaption").html($('.img-caption', this).html());
$(document).ready(function() {
$('a.post-carousel').on('click', function() {
$('#expandedImg').attr('src', $('.carousel-img', this).attr('src')).parent().show();
$("#expandedImgCaption").html($('.img-caption', this).html());
});
});
* {
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%;
}