简体   繁体   English

jQuery我做错了什么?

[英]Jquery What have I done wrong?

$(document).ready(function(){
   $('.fa').hide();
   $('.icon').click (function(){
     $('.icon').addClass('active');
     if($(".fa").css("display") == "none") {
       $(".fa").show();
     }
     if $('.icon').click && $('.icon').hasCalss('active') (function(){
       $(".fa").hide();
     });
   });
});

I want it so that when you click on a div(in this case '.icon') The div .fa shows but when I click on it again and .fa is showing it hides .fa In the console it keeps on coming up with these 2 errors 我想要它,以便当您单击div(在本例中为“ .icon”)时显示div .fa,但是当我再次单击它并显示.fa时它就会隐藏.fa在控制台中,它会继续显示这两个错误

Uncaught SyntaxError: Unexpected identifier SyntaxError: Unexpected identifier 未捕获的SyntaxError:意外的标识符SyntaxError:意外的标识符

but I don't know whats wrong as i'm quite new to jquery and java-script. 但是我不知道怎么了,因为我对jQuery和Java脚本还很陌生。 Help would be appreciated. 帮助将不胜感激。 Thank you :) 谢谢 :)

You need to use toogle function : 您需要使用toogle函数:

$(document).ready(function(){
   $('.fa').hide();
   $('.icon').click (function(){
     $('.icon').addClass('active');
     $(".fa").toogle();
   });
});

https://www.w3schools.com/jquery/eff_toggle.asp https://www.w3schools.com/jquery/eff_toggle.asp

If it is just about switching classes on click you can use the .toggleClass() -method: Link to toggleClass() on http://api.jquery.com . 如果仅是单击时切换类,则可以使用.toggleClass() -方法: 链接到http://api.jquery.com上的toggleClass() Just use your CSS-Class to manipulate the state. 只需使用CSS类来操纵状态即可。

 $(document).ready(function(){ const icon = $('.icon'); icon.click(function() { $(this).toggleClass("active"); }); }); 
 .container { border: 1px solid grey; padding: 0.5em; text-align: center; } .icon { opacity: 0.5; font-size: 48px; color: #ddd; transition: all 300ms linear; cursor: pointer; } .icon.active { opacity: 1; color: #bada55; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" rel="stylesheet"/> <section class="container"> <span class="icon active"> <i class="fas fa-stroopwafel"></i> </span> <span class="icon"> <i class="fas fa-balance-scale"></i> </span> </section> 

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

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