简体   繁体   中英

JQUERY, When an LI is clicked and the click was not on an <A href> Toggle

I'm interested in having my LIs toggle when a user clicks in the blank area of the LI. The LI does contain links that redirect, but in the case when the user clicks on a blank area of the LI I would like the LI to toggle

<li id="58" class="records">
<a href="adadad">Title</a>
<div class="expanded" style="display:none;" id="expanded_58"> STUFF </DIV>
</li>

Example, with the code above, if the user clicks on the LI, and that click isn't on the HREF, I would like the expanded_58 to Toggle.

Thanks!

Use this:

$(function(){
    $('li.records').click(function(e){
       if(e.target == this) $('div.expanded', this).toggle();
    });
});
$("li.records").click(function(e){
  if (e.target.nodeName == "LI")
    $(".expanded", this).toggle();
});

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