[英]Click event to fire only on one element of a class
I have this layout with cards where each card has this markup: 我的卡片布局如下,其中每张卡片都有此标记:
<div class="card">
<div class="content">
<div class="brand">
<img src="/assets/logo.png" alt="">
</div>
<div class="name">
<h3>Lorem ipsum</h3>
</div>
<div class="price">
25€/year
</div>
<div class="buttons">
<a href="#" class="btn btn-color">Buy</a>
<a href="#" class="btn btn-ghost overlay-toggle">Details</a>
</div>
</div>
<div class="details">
...Details here...
</div>
</div>
When I click the details button I want the details div to pop over the card. 当我单击详细信息按钮时,我希望详细信息div在卡片上弹出。 I have this working as I want, but the problem is that the event fires on all the cards at once instead of just the one I clicked the button from. 我可以根据需要进行此工作,但问题是该事件会立即在所有卡上触发,而不仅仅是我单击按钮时触发的事件。
This is the jQuery for the click: 这是单击的jQuery:
var overlayToggle = $('.overlay-toggle');
var overlayClose = $('.close-overlay');
var overlay = $('.details');
overlayToggle.on('click', (e) => {
e.preventDefault();
overlay.css({
'visibility': 'visible',
'opacity': '1',
'transform': 'scale(1)'
});
});
overlayClose.on('click', () => {
overlay.css({
'visibility': 'hidden',
'opacity': '0',
'transform': 'scale(0)'
});
});
The problem is that you have defined overlay
as a JQuery wrapped set that contains all the details
elements and in each event handler, you tell the entire set to display or hide. 问题是,你已经定义overlay
作为一个jQuery包裹包含所有设置details
元素,并在每个事件处理程序,你告诉整个设置显示或隐藏。
What you need to do is, in the event handlers, find only the details
element that corresponds to the button that was clicked. 您需要做的是,在事件处理程序中,仅找到与单击的按钮相对应的details
元素。
var overlayToggle = $('.overlay-toggle'); var overlayClose = $('.close-overlay'); overlayToggle.on('click', (e) => { e.preventDefault(); // Find the .details within nearest ancestor that is .card $(".details", $(e.target).closest(".card")).css({ 'visibility': 'visible', 'opacity': '1', 'transform': 'scale(1)' }); }); overlayClose.on('click', () => { overlay.css({ 'visibility': 'hidden', 'opacity': '0', 'transform': 'scale(0)' }); });
.details { visibility:hidden; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="card"> <div class="content"> <div class="brand"> <img src="/assets/logo.png" alt=""> </div> <div class="name"> <h3>Lorem ipsum</h3> </div> <div class="price"> 25€/year </div> <div class="buttons"> <a href="#" class="btn btn-color">Buy</a> <a href="#" class="btn btn-ghost overlay-toggle">Details</a> </div> </div> <div class="details"> ...Details here... </div> </div> <div class="card"> <div class="content"> <div class="brand"> <img src="/assets/logo.png" alt=""> </div> <div class="name"> <h3>Lorem ipsum</h3> </div> <div class="price"> 25€/year </div> <div class="buttons"> <a href="#" class="btn btn-color">Buy</a> <a href="#" class="btn btn-ghost overlay-toggle">Details</a> </div> </div> <div class="details"> ...Details here... </div> </div> <div class="card"> <div class="content"> <div class="brand"> <img src="/assets/logo.png" alt=""> </div> <div class="name"> <h3>Lorem ipsum</h3> </div> <div class="price"> 25€/year </div> <div class="buttons"> <a href="#" class="btn btn-color">Buy</a> <a href="#" class="btn btn-ghost overlay-toggle">Details</a> </div> </div> <div class="details"> ...Details here... </div> </div>
overlayToggle.on('click', (e) => {
e.preventDefault();
$(e.target).parents('.card').find('.details').css({
'visibility': 'visible',
'opacity': '1',
'transform': 'scale(1)'
});
});
overlayClose.on('click', () => {
e.preventDefault();
$(e.target).parents('.card').find('.details').css({
'visibility': 'hidden',
'opacity': '0',
'transform': 'scale(0)'
});
});
You can also use like this, the problem is you haven't pointed to which details block you need to display. 您也可以这样使用,问题是您没有指出需要显示的详细信息块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.