简体   繁体   中英

Fire onClick event to specific div

I have two cards and I am firing an onClick() event to create a dimmer on top of the card image if a user clicks on Like button (heart icon). I am trying to create the dimmer on the card image in which user clicked the heart icon.

I tried placing a condition :

if (e.target !== this)
    return;

But it did not work. In real webpage, I will have a long list of cards with no identifier where I can specify the image to be dimmed. Is it possible in this scenario?

JSFiddle

Updated fiddle .

You should use the current clicked jQuery object $(this) instead to target the related .imglove in the same .card :

$(this).closest('.card').find('.imglove').dimmer('show');

NOTE : The .closest('.card') is used to get the related card with clicked icon loveit .

Hope this helps.

 $(function() { $(document).on('click', '.loveit', function(e) { e.preventDefault(); $(this).closest('.card').find('.imglove').dimmer('show'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/semantic-ui/2.2.2/semantic.min.js"></script> <link href="https://cdn.jsdelivr.net/semantic-ui/2.2.2/semantic.min.css" rel="stylesheet"/> <div class="ui card"> <div class="content"> <div class="right floated meta">14h</div> <img class="ui avatar image" src="http://semantic-ui.com/images/avatar/large/elliot.jpg"> Elliot </div> <div class="image imglove"> <div class="ui dimmer"> <div class="content"> <div class="center"> <h2 class="ui inverted header">Title</h2> <img class="ui avatar image" src="http://www.animatedimages.org/data/media/373/animated-heart-image-0455.gif"> </div> </div> </div> <img src="http://semantic-ui.com/images/avatar2/large/kristy.png"> </div> <div class="content"> <span class="right floated"> <i class="heart outline like icon loveit"></i> 17 likes </span> <i class="comment icon"></i> 3 comments </div> <div class="extra content"> <div class="ui large transparent left icon input"> <i class="heart outline icon"></i> <input type="text" placeholder="Add Comment..."> </div> </div> </div> <div class="ui card"> <div class="content"> <div class="right floated meta">14h</div> <img class="ui avatar image" src="http://semantic-ui.com/images/avatar/large/elliot.jpg"> Elliot </div> <div class="image imglove"> <div class="ui dimmer"> <div class="content"> <div class="center"> <h2 class="ui inverted header">Title</h2> <img class="ui avatar image" src="http://www.animatedimages.org/data/media/373/animated-heart-image-0455.gif"> </div> </div> </div> <img src="http://semantic-ui.com/images/avatar2/large/kristy.png"> </div> <div class="content"> <span class="right floated"> <i class="heart outline like icon loveit"></i> 17 likes </span> <i class="comment icon"></i> 3 comments </div> <div class="extra content"> <div class="ui large transparent left icon input"> <i class="heart outline icon"></i> <input type="text" placeholder="Add Comment..."> </div> </div> </div> 

So you're trying to limit your interaction to the currently selected image? Also, rather than reference a fiddle, post your working (or non-working) code here. Easier all around.

 jQuery(document).ready(function($) { $(document).on('click', '.loveit', function(e) { e.preventDefault(); $(this).parents(".card").find('.imglove') .dimmer('show'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://cdn.jsdelivr.net/semantic-ui/2.2.2/semantic.min.css" rel="stylesheet"/> <script src="https://cdn.jsdelivr.net/semantic-ui/2.2.2/semantic.min.js"></script> <div class="ui card"> <div class="content"> <div class="right floated meta">14h</div> <img class="ui avatar image" src="http://semantic-ui.com/images/avatar/large/elliot.jpg"> Elliot </div> <div class="image imglove"> <div class="ui dimmer"> <div class="content"> <div class="center"> <h2 class="ui inverted header">Title</h2> <img class="ui avatar image" src="http://www.animatedimages.org/data/media/373/animated-heart-image-0455.gif"> </div> </div> </div> <img src="http://semantic-ui.com/images/avatar2/large/kristy.png"> </div> <div class="content"> <span class="right floated"> <i class="heart outline like icon loveit"></i> 17 likes </span> <i class="comment icon"></i> 3 comments </div> <div class="extra content"> <div class="ui large transparent left icon input"> <i class="heart outline icon"></i> <input type="text" placeholder="Add Comment..."> </div> </div> </div> <div class="ui card"> <div class="content"> <div class="right floated meta">14h</div> <img class="ui avatar image" src="http://semantic-ui.com/images/avatar/large/elliot.jpg"> Elliot </div> <div class="image imglove"> <div class="ui dimmer"> <div class="content"> <div class="center"> <h2 class="ui inverted header">Title</h2> <img class="ui avatar image" src="http://www.animatedimages.org/data/media/373/animated-heart-image-0455.gif"> </div> </div> </div> <img src="http://semantic-ui.com/images/avatar2/large/kristy.png"> </div> <div class="content"> <span class="right floated"> <i class="heart outline like icon loveit"></i> 17 likes </span> <i class="comment icon"></i> 3 comments </div> <div class="extra content"> <div class="ui large transparent left icon input"> <i class="heart outline icon"></i> <input type="text" placeholder="Add Comment..."> </div> </div> </div> 

Target it parent with class card then find class imglove and add dimmer to it

 jQuery(document).ready(function($) {
   $(document).on('click', '.loveit', function(e) {
     $(this).parents('.card').find('.imglove').dimmer('show');
    });
 });

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM