简体   繁体   English

jQuery代码无法正常运行

[英]jQuery code not working as I want

Sorry about the title I just don't know what to set it as. 对不起标题,我只是不知道将其设置为什么。 If you can think of a title for this question please tell me or edit, thanks. 如果您能想到该问题的标题,请告诉我或编辑,谢谢。 Now to the question. 现在到问题。 I have written some jQuery code which displays different boxes depending on which link is clicked. 我已经写了一些jQuery代码,根据单击的链接显示不同的框。 And when you click the link again all the boxes reappear which is exactly what I want. 当您再次单击链接时,所有方框都会重新出现,这正是我想要的。 My problem is that when I click link1 then link2 and then go back to link1 all the images reappear. 我的问题是,当我单击link1时,然后单击link2,然后返回到link1,所有图像重新出现。 I don't want this to happen. 我不希望这种情况发生。 I only want the boxes for link1 to appear. 我只希望出现link1的框。 All the boxes should only show at the start and when you click a link twice. 所有框应仅在开始时和单击链接两次时显示。

 $(".links").click(function(){ var current_link = $(this); var images = $(".images") $(images).removeClass('hide'); $(images).removeClass('show'); var current = $(this).data("cat"); $(images).addClass('hide'); $('.'+current).addClass('show'); $(current_link).click(function(){ $(images).toggleClass('hide'); }); }); 
 ul{ list-style-type: none; } ul li{ display: inline-block; } ul li a{ font-size: 20px; padding: 10px; background-color: yellow; } .change{ background-color: red; } .print{ height: 100px; width: 100px; background-color: blue; display: inline-block; } .portfolio{ height: 100px; width: 100px; background-color: red; display: inline-block; } .web{ height: 100px; width: 100px; background-color: orange; display: inline-block; } .hide{ display: none; } .show{ display: inline-block; } 
 <ul> <li><a id="hi" class="links" data-cat="print" href="#">link1</a></li> <li><a class="links" data-cat="portfolio" href="#">link2</a></li> <li><a class="links" data-cat="web" href="#">link3</a></li> </ul> <div class="print images"> </div> <div class="portfolio images"> </div> <div class="web images"> </div> <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script> 

You can add a condition on the links by adding a class .active and check if the link has that class, and use just the class .hide for the .images , no need to use class .show 您可以通过添加一个类。主动上的链接添加条件,并检查链接都有课,并且只使用了.images.hide,没有必要使用类.show

See code snippet : 参见代码片段

 $(".links").click(function() { var current_link = $(this); var images = $(".images"); var links = $(".links"); var current = $(this).data("cat"); if (current_link.hasClass('active')) { links.removeClass('active'); images.removeClass('hide'); } else { links.removeClass('active'); current_link.addClass('active'); images.addClass('hide'); $('.' + current).removeClass('hide'); } }); 
 ul { list-style-type: none; } ul li, .images { display: inline-block; } ul li a { font-size: 20px; padding: 10px; background-color: yellow; } .change { background-color: red; } .images { height: 100px; width: 100px; } .print { background-color: blue; } .portfolio { background-color: red; } .web { background-color: orange; } .hide { display: none; } 
 <ul> <li><a id="hi" class="links" data-cat="print" href="#">link1</a></li> <li><a class="links" data-cat="portfolio" href="#">link2</a></li> <li><a class="links" data-cat="web" href="#">link3</a></li> </ul> <div class="print images"></div> <div class="portfolio images"></div> <div class="web images"></div> <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script> 

You need to maintain the current link in global variable or like I have done it inside the window object, Try this: 您需要维护全局变量中的当前链接,或者像我在window对象中所做的那样,请尝试以下操作:

 $(".links").click(function(){ var current_link = $(this); var images = $(".images") var current = $(this).data("cat"); if(current == window.currentImage){ $(images).removeClass('hide'); $(images).removeClass('show'); window.currentImage = null; return; // don't do anything id current link is clicked } $(images).addClass('hide'); $('.'+current).removeClass('hide'); $('.'+current).addClass('show'); window.currentImage = current; }); 
 ul{ list-style-type: none; } ul li{ display: inline-block; } ul li a{ font-size: 20px; padding: 10px; background-color: yellow; } .change{ background-color: red; } .print{ height: 100px; width: 100px; background-color: blue; display: inline-block; } .portfolio{ height: 100px; width: 100px; background-color: red; display: inline-block; } .web{ height: 100px; width: 100px; background-color: orange; display: inline-block; } .show{ display: inline-block; } .hide{ display: none; } 
 <ul> <li><a id="hi" class="links" data-cat="print" href="#">link1</a></li> <li><a class="links" data-cat="portfolio" href="#">link2</a></li> <li><a class="links" data-cat="web" href="#">link3</a></li> </ul> <div class="print images"> </div> <div class="portfolio images"> </div> <div class="web images"> </div> <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script> 

Thanks 谢谢

This could be a cleaner solution for you. 对于您来说,这可能是一个更清洁的解决方案。 i am also using a condition :) 我也在用一个条件:)

 var current = null; var images = $('.images'); $(".links").click(function(){ if(current === $(this).data("cat")){ images.removeClass('hide'); current = null; } else { current = $(this).data("cat"); $(images).addClass('hide'); $('.'+current).removeClass('hide'); } }); 
 ul{ list-style-type: none; } ul li{ display: inline-block; } ul li a{ font-size: 20px; padding: 10px; background-color: yellow; } .change{ background-color: red; } .print{ height: 100px; width: 100px; background-color: blue; display: inline-block; } .portfolio{ height: 100px; width: 100px; background-color: red; display: inline-block; } .web{ height: 100px; width: 100px; background-color: orange; display: inline-block; } .hide{ display: none; } .show{ display: inline-block; } 
 <ul> <li><a id="hi" class="links" data-cat="print" href="#">link1</a></li> <li><a class="links" data-cat="portfolio" href="#">link2</a></li> <li><a class="links" data-cat="web" href="#">link3</a></li> </ul> <div class="print images"> </div> <div class="portfolio images"> </div> <div class="web images"> </div> <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script> 

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

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