简体   繁体   English

使用jquery隐藏/显示div时,链接停止工作

[英]Links stop working when using jquery to hide/show divs

I've succeeded in getting divs to show/hide but now for some reason, all other links on the page stop working unless if I open them in a new tab. 我已经成功显示/隐藏了div,但是由于某种原因,除非我在新标签页中打开它们,否则页面上的所有其他链接都会停止工作。 How can I get this fixed? 如何解决此问题?

 $(document).ready(function () { $("#div9").show("slow").siblings().hide("slow"); $('a').click(function () { event.preventDefault(); var divname = this.name; $("#" + divname).show("slow").siblings().hide("slow"); }); $('#seeAll').click(function() { $('#div2').show("slow") $('#div3').show("slow") $('#div4').show("slow") $('#div5').show("slow") $('#div7').show("slow") $('#div8').show("slow") $('#div9').hide("slow") }) }); 
  <a class="people-letters" href="#" name="div2">A</a> <a class="people-letters" href="#" name="div3">B</a> <a class="people-letters" href="#" name="div4">C</a> <a class="people-letters" href="#" name="div5">D</a> EFGHIJKLMNOPQR <a class="people-letters" href="#" name="div7">S</a> TUV <a class="people-letters" href="#" name="div8">W</a> XYZnbsp;<a id="seeAll" class="people-letters" href="#">See All</a> <div> <div id="div2" style="display: none;"> div2 </div> <div id="div3" style="display: none;"> div3 </div> <div id="div4" style="display: none;"> div4 </div> <div id="div5" style="display: none;"> div5 </div> <div id="div7" style="display: none;"> div7 </div> <div id="div8" style="display: none;"> div8 </div> <div id="div9" style="display: none;"> div9 </div> </div> <a href="http://google.com/">Sample Link</a> 

EDIT: 编辑:

You have bound your click event to ALL link elements by specifying 'a' as your selector. 通过将“ a”指定为选择器,已将click事件绑定到ALL链接元素。 in order to bind it only to that specific link you can use your class that you've specified for the links that hide and show the div elements. 为了仅将其绑定到该特定链接,可以使用为隐藏和显示div元素的链接指定的类。

$('a.people-letters').click(function (event) {
    event.preventDefault();
    var divname = this.name;
    $("#" + divname).show("slow").siblings().hide("slow");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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