简体   繁体   English

WordPress中的JS / Jquery toggleClass

[英]JS/Jquery toggleClass in WordPress

I'm trying to integrate a simple toggleClass function on my main home page in WordPress. 我正在尝试在WordPress的主主页上集成一个简单的toggleClass函数。

I have a built for my main navigation links, and on two of the links I am trying to have it so when you click them a hidden div toggles the class hideMe to turn on and off display: none 我为主要的导航链接构建了一个,在两个链接中,我都尝试使用它,因此当您单击它们时,一个隐藏的div切换了hideMe类以打开和关闭显示:无

The code for the javascript is as follows, I saw online that you can't use $ and instead have to use jQuery javascript的代码如下,我在网上看到您不能使用$,而必须使用jQuery

  $(document).ready(function () { function cntct(){ jQuery('#contactBx').toggleClass('hideMe'); //$('#contactBx').toggleClass('hideMe'); //alert('hey'); }); }); 
 .hidCnt{ width: 250px !important; height: 200px !important; background-color: red !important; z-index: 10 !important; position: absolute; top: 19%; left: 60%; display: none; } .hideMe{ display: block !important; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav class="site-nav"> <a class="nav-link active" href="anthonycarreramusic.com">Home</a> <a class="nav-link" href="#aboutAnch">About</a> <a class="nav-link" href="shows">Shows</a> <a class="nav-link" href="media">Gallery</a> <a class="nav-link" id="contactLink" runat="server" onClick="cntct();">Contact</a> <a class="nav-link" id="socialLink" runat="server">Social</a> </nav> <div class="hidCnt" id="contactBx"> This is my test contact form region </div> 

For some reason I can't get any of it to trigger, (the alert or the toggleClass) and was hoping someone could point out what I might be missing to make this function correctly. 由于某种原因,我无法触发它(警报或toggleClass),并希望有人指出我可能无法正确执行此功能。 Thanks for any and all information! 感谢您提供所有信息!

Your cntct() is inside multiple functions and is not exposed to window so on click of the <a will not have any clue where the function is and will result in an error. 您的cntct()在多个函数中,并且没有暴露于窗口中,因此单击<a不会知道该函数的位置,并且会导致错误。

You can instead bind a click event to id contactLink which is the same element on which you are doing onclick . 您可以改为将click事件绑定到id contactLink ,该ID与您在onclick上使用的元素相同。

$(document).ready(function () {
   $("#contactLink").on("click", function(){
     jQuery('#contactBx').toggleClass('hideMe');
   });
});

You should create your function out of $(document).ready() . 您应该使用$(document).ready()创建函数。 And you can tell that, in this case, there is no need to use $(document).ready() . 而且您可以说,在这种情况下,不需要使用$(document).ready()

 function cntct(){ jQuery('#contactBx').toggleClass('hideMe'); } 
 .hidCnt{ width: 250px !important; height: 200px !important; background-color: red !important; z-index: 10 !important; position: absolute; top: 19%; left: 60%; display: none; } .hideMe{ display: block !important; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav class="site-nav"> <a class="nav-link active" href="anthonycarreramusic.com">Home</a> <a class="nav-link" href="#aboutAnch">About</a> <a class="nav-link" href="shows">Shows</a> <a class="nav-link" href="media">Gallery</a> <a class="nav-link" id="contactLink" runat="server" onClick="cntct();">Contact</a> <a class="nav-link" id="socialLink" runat="server">Social</a> </nav> <div class="hidCnt" id="contactBx"> This is my test contact form region </div> 

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

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