gpt4 book ai didi

javascript - onClick anchor 标记当前 div 使用 jquery 显示

转载 作者:可可西里 更新时间:2023-11-01 13:42:27 26 4
gpt4 key购买 nike

我在这里处理图片库 我希望 onClick anchor 标记当前图像在 .gallery-container 中显示不起作用。之前在图像 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);
});

这段代码运行良好。现在我添加了 anchor ,但它不起作用。 我已经尝试过但无法实现。

有人可以推荐我吗?

$(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>

最佳答案

尝试按以下方式使用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>

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com