简体   繁体   English

jQuery 代码段在 WordPress 网站上不起作用

[英]jQuery Snippet Not Working on WordPress Site

WordPress Version - 6.3.1 WordPress 版本 - 6.3.1

Theme - JointsWP (blank starter theme)主题 - JointsWP(空白入门主题)

jQuery Version - 6.3.3 (tried 6.3.1 also) jQuery 版本 - 6.3.3(也试过 6.3.1)

Website Link - https://legiondesigns.com/dev/labella/网站链接 - https://legiondesigns.com/dev/labella/

EDIT : There are NO console errors either编辑:也没有控制台错误

I seem to be having a problem on my WordPress site.我的 WordPress 网站似乎有问题。 When running a jquery script in codepen.io everything functions properly.在 codepen.io 中运行 jquery 脚本时,一切正常。 I AM using this code snippet on another site that I have designed years ago and it works fine using jQuery 6.3.1 but When running that code through my website it is not function properly at all.我在我多年前设计的另一个网站上使用这个代码片段,它使用 jQuery 6.3.1 运行良好,但是当通过我的网站运行该代码时,它根本不是 function。

Code snippet in question:有问题的代码片段:

jQuery(document).ready(function( $ ){
 $('.mobileBtn').click(function(){
  if( $('.mobileNav').hasClass('animate__zoomIn') ){
    $('.mobileNav').removeClass('animate__zoomIn').addClass('animate__zoomOut');
    $('.mobileBtn').removeClass('btnActive');
    setTimeout(function () {
      $('.mobileNav').css({display: 'none'}).removeClass('animate__zoomOut')
    },1000 );
    // alert('1');
  }
  else{
    $('.mobileNav').css({display: 'block'}).addClass('animate__zoomIn');
    $('.mobileBtn').addClass('btnActive');
    // alert('2');
  }
});
}); 

When I load this up on codepen.io ( https://codepen.io/agon024/pen/KKBxNwx ) it functions like it should.当我在 codepen.io ( https://codepen.io/agon024/pen/KKBxNwx ) 上加载它时,它的功能就像它应该的那样。

I have disabled all the plugins and it still does it.我已经禁用了所有插件,但它仍然可以。 I have removed all other jQuery from the script and it still does it.我已经从脚本中删除了所有其他 jQuery,它仍然可以。 I have tried using "jQuery" instead of "$" and nothing.我试过使用“jQuery”而不是“$”但什么也没有。 I am using this starter theme on another site and it doesn't have a problem.我在另一个网站上使用这个入门主题,没有问题。

IMPORTANT - If I uncomment the "alert" lines I see that it is looping through the "if" and "else" statements without stopping.重要- 如果我取消注释“警报”行,我会看到它正在不停地循环“if”和“else”语句。 When I click "mobileBtn" it adds the class "animate__zoomIn" and pops up an alert saying "2" then when I click OK on the alert it instantly removes the class "animate__zoomIn" and adds the class "animate__zoomOut", sets the inline style to "display: none" after 1000ms and pops up an alert saying "1" and when I click OK it stops.当我单击“mobileBtn”时,它会添加 class“animate__zoomIn”并弹出一个提示“2”,然后当我在警报上单击“确定”时,它会立即删除 class“animate__zoomIn”并添加 class“animate__zoomOut”,设置内联样式在 1000 毫秒后显示为“无”,并弹出一条警告“1”,当我单击“确定”时,它停止了。

Anyone know what is going on?有人知道发生了什么事吗? Like I said it seems to be looping throught the "if" and "else" statements even when the "else" condition is met.就像我说的,即使满足“else”条件,它似乎也在循环“if”和“else”语句。 This is the first time I have encountered this and seeing as it works on codepen it must have something to do with WordPress.这是我第一次遇到这个问题,看到它在 codepen 上工作,它一定与 WordPress 有关。

This should work as desired.这应该按需要工作。

 $('.mobileNav').addClass('animate__zoomOut').hide(); $('.mobileBtn').click(function(){ $('.mobileNav').show(); if ($(this).hasClass('btnNotActive')) { $(this).toggleClass('btnActive btnNotActive'); $('.mobileNav').toggleClass('animate__zoomIn animate__zoomOut'); } else { $('.mobileNav').toggleClass('animate__zoomIn animate__zoomOut'); $(this).toggleClass('btnActive btnNotActive'); } $(this).css('pointer-events', 'none'); setTimeout(function(){ $('.mobileBtn').css('pointer-events', 'auto'); }, 1000); });
 .mobileNav { background-color: red; width: 400px; height: 400px; }.mobileBtn { display: inline-block; padding: 10px 50px; background-color: blue; color: white; width: auto; margin: 0 auto; }
 <link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script> <div class="mobileBtn btnNotActive show-for-small-only"> Click Me </div> <div class="mobileNav animate__animated"> <div class="mobileNavInner"> <?php joints_off_canvas_nav(); ?> </div> </div>

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

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