Jquery:当您单击并隐藏其他div时显示相关的div(Jquery: Show related div when you click and hide others)

编程入门 行业动态 更新时间:2024-10-18 23:25:54
Jquery:当您单击并隐藏其他div时显示相关的div(Jquery: Show related div when you click and hide others)

我有2个不同内容的div。 我想点击链接时显示相关的div。 它现在显示两个div,它应该只显示相关的div。

的jsfiddle

$('div.tip-2').hide(); $('.showDiv').on('click', function() { $('div.tip-2').fadeIn(200); event.stopPropagation(); }); $(document).click(function(e) { if (!$(e.target).is('div.tip-2 *')) { $("div.tip-2").fadeOut(200); } });

HTML

<a class="showDiv" href="#">Show div</a> <div class="tip-2"> <p>This is one!</p> </div> <br/><br/> <a class="showDiv" href="#">Show div</a> <div class="tip-2"> <p>This is two!</p> </div>

I have 2 divs with different content. I want to show the related div when I click on the link. It's showing both divs at the moment and it was supposed to show only the related one.

jsfiddle

$('div.tip-2').hide(); $('.showDiv').on('click', function() { $('div.tip-2').fadeIn(200); event.stopPropagation(); }); $(document).click(function(e) { if (!$(e.target).is('div.tip-2 *')) { $("div.tip-2").fadeOut(200); } });

HTML

<a class="showDiv" href="#">Show div</a> <div class="tip-2"> <p>This is one!</p> </div> <br/><br/> <a class="showDiv" href="#">Show div</a> <div class="tip-2"> <p>This is two!</p> </div>

最满意答案

试试这个

$('div.tip-2').hide(); $('.showDiv').on('click', function() { $("div.tip-2").hide(); $(this).next().fadeIn(200); event.stopPropagation(); }); $(document).click(function(e) { if (!$(e.target).is('div.tip-2 *')) { $("div.tip-2").fadeOut(200); } });

更新小提琴

Try this

$('div.tip-2').hide(); $('.showDiv').on('click', function() { $("div.tip-2").hide(); $(this).next().fadeIn(200); event.stopPropagation(); }); $(document).click(function(e) { if (!$(e.target).is('div.tip-2 *')) { $("div.tip-2").fadeOut(200); } });

updated Fiddle

更多推荐

本文发布于:2023-08-01 02:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1350593.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当您   单击   div   Jquery   click

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!