- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在这里处理图片库 我希望 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/
这里有这样的问题,但我尝试了解决方案,但在我的情况下这不起作用。我有一个通过单击打开的表单,然后必须通过单击此表单内的 x 将其关闭。 This variant不适合,因为 cookie 在关闭表单后
我有以下代码: function sdefaults() { alert("test"); } var btnpos, sbtn; btnpos = document.getElementsBy
我正在学习 javascript 和 php。我创建了一个联系表单,我希望按下提交按钮时可以完成两件事: 将数据提交给我(这部分正在运行) 阅读我的 onclick 函数(这部分不起作用) 我正在
我的页面有多个div元素,有显示切换功能。 onclick = "document.getElementById('light').style.display='block';document.ge
为什么不同? document.getElementById('click').onclick = a.replaceChild(c,b) --- 在我不点击按钮的情况下替换元素 但 onclick=
我以为在javascript中绑定(bind)点击事件是通过node.onclick来完成的,Chrome/Firefox似乎也同意我的看法,但我看到它写成.onClick here 3个人4次,所以
基本上,我想创建一个侧边栏,其中会有很多联系人。每个联系人都会出现在一个列表中。列表中有两个 onclick 函数。列表中两个 onclick 函数是:第一次点击-点击列表后它应该打开另一个侧边栏(我
我是 Javascript 的新手,尤其是在 JSON 和 Jquery 上。我已经成功实现了 FullCalendar 的月 View ,但是无论如何要根据用户单击月 View 的日期来显示或弹出日
给定一些简单的 HTML,例如,其中一个元素具有 onclick 函数,并且它的子元素也具有 onclick 函数: 正确的方法是什么,以便当用户单击子元素时,仅执行子元素的 oncli
我有一个功能,当您单击一个按钮时,TinyMCE 框内的选定文本被包装在 span 中标签。 这是这样做的: var apolo = '' + sel + ''; tinyMCE.activeEdit
我尝试使用 JavaScript 将 onclick 事件替换为其他 onclick 事件: 旧 onClick 事件: $('#myButton').click(function(){ a
我正在尝试删除一个函数中的事件监听器,该函数与 onclick 调用的函数相同。 下面的例子说明了我的问题。我尝试使用这种构造的原因是将函数传递给 View 对象,以便它可以将其作为 onclick
这是一个相当简单的可访问性问题。 假设我们有一个切换链接/按钮,单击该链接/按钮即可切换移动菜单。对于这样的按钮,哪种格式更易于访问? 我们总共有 4 个选项,用 分隔 button标签与 a标签,
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How to make onclick automatically through onload function
我需要能够改变一个 id 的 onclick 事件,这样一旦它被点击,一旦它执行一个改变 onclick 事件的函数 这是我的代码:Javascript: function showSearchBar
有谁知道如何将 onClick 分配给执行一个操作的对象,然后当用户点击同一对象时,该操作被反转? $('#image').click(function() { $('#foo').css({
我重写了 onClick(View) 方法,以便在单击时 View 大小会发生变化。View 是一个显示浏览器网页的 webView。我需要点击会改变大小,而且浏览器会响应(转到被点击的链接)。在目前
我有这个 HTML 代码: APPLY JS: $('#changeType').click(function(){ $('#discountCode').attr('type','pass
我想用 onclick 创建一个函数,如果我按下 div 就会显示内容。如果我点击该内容,它会给我“开始”表单。 让我向您展示代码: HTML: click Javascript: var div =
我编写了一个函数,用于使用超链接的占位符打开/关闭 div。 Open 然后 onclick 事件打开 div。 我有这个onclick来关闭它: Close 我想要做的是为在两个 onclick 事
我是一名优秀的程序员,十分优秀!