简体   繁体   English

为什么我的 click() function 仅适用于调整大小?

[英]Why is my click() function working only on resize?

I am getting insane.我快疯了。 As you can see at below code snippet - everything works well.正如您在下面的代码片段中看到的 - 一切正常。 But not in my project.但不在我的项目中。 In my project works only code in resize().在我的项目中,仅适用于 resize() 中的代码。 When I resize window - ok, it's nice, I can add and remove class 'open' by click on item.当我调整 window 的大小时 - 好的,很好,我可以通过单击项目添加和删除 class '打开'。 But if I refresh page and not resize window - I cant't add class.但是如果我刷新页面而不调整 window 的大小 - 我不能添加 class。 Can't do nothing.不能什么都不做。 I am spending few hours with this thing.我在这件事上花了几个小时。 Can somebody help me?有人可以帮助我吗? Why isn't it working?为什么它不起作用?

 (function ($) { $('.nav-item').on('click', function() { $(this).toggleClass('open'); $(this).siblings().removeClass('open'); }); $(window).resize(function() { if($(window).width() < 1200) { $('.nav-item').on('click', function() { $(this).toggleClass('open'); $(this).siblings().removeClass('open'); }); } }); })(jQuery);
 .open { color: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="main-menu"> <li class="nav-item"> <span class="nav-link">Menu item 1</span> </li> <li class="nav-item"> <span class="nav-link">Menu item 2</span> <ul class="dropdown-menu"> <li class="dropdown-item"> <span>Menu item 2 subitem 1</span> </li> <li class="dropdown-item"> <span>Menu item 2 subitem 2</span> </li> </ul> </li> <li class="nav-item"> <span class="nav-link">Menu item 3</span> <ul class="dropdown-menu"> <li class="dropdown-item"> <span>Menu item 3 subitem 1</span> </li> <li class="dropdown-item"> <span>Menu item 3 subitem 2</span> </li> </ul> </li> </ul>

I work with Drupal 8 CMS (if it's important - but I worked with jQuery, click() and Drupal milion of times and it was always fine.我使用 Drupal 8 CMS(如果它很重要的话——但我使用了 jQuery、click() 和 Drupal 数百万次,而且总是很好。

The reason it works in the snippet but not your code is because the SO snippets automatically place jQuery logic within a document.ready event handler, which ensures that the DOM has loaded before the code runs.它在代码片段中起作用但在您的代码中不起作用的原因是,SO 片段会自动将 jQuery 逻辑放置在document.ready事件处理程序中,从而确保在代码运行之前已加载 DOM。

However your code is running in an IIFE , not a document.ready event handler, so you try to bind the events to elements that don't exist yet.但是,您的代码在IIFE中运行,而不是在 document.ready 事件处理程序中运行,因此您尝试将事件绑定到尚不存在的元素。 It's not until the resize event occurs that the event is bound and starts working.直到resize事件发生,该事件才被绑定并开始工作。

To fix this replace the IIFE with a document.ready handler:要解决此问题,请将 IIFE 替换为 document.ready 处理程序:

 jQuery(function($) { $('.nav-item').on('click', function() { $(this).toggleClass('open').siblings().removeClass('open'); }); })
 .open { color: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="main-menu"> <li class="nav-item"> <span class="nav-link">Menu item 1</span> </li> <li class="nav-item"> <span class="nav-link">Menu item 2</span> <ul class="dropdown-menu"> <li class="dropdown-item"> <span>Menu item 2 subitem 1</span> </li> <li class="dropdown-item"> <span>Menu item 2 subitem 2</span> </li> </ul> </li> <li class="nav-item"> <span class="nav-link">Menu item 3</span> <ul class="dropdown-menu"> <li class="dropdown-item"> <span>Menu item 3 subitem 1</span> </li> <li class="dropdown-item"> <span>Menu item 3 subitem 2</span> </li> </ul> </li> </ul>

I searched another few hours in the Internet and I found a solution with Drupal.behaviours... I don't know how and why it works, why in this project this simple jQuery code (from below and from my post, it's the same) didn't work, but in another Drupal projects works well... I don't know.我又在网上搜索了几个小时,我找到了一个解决方案 Drupal.behaviours... ) 没用,但在另一个 Drupal 项目中运行良好......我不知道。 Maybe someone could tell me.也许有人可以告诉我。 I don't know.我不知道。

Version with:版本:

jQuery(function($) {
  ...
});

instead of:代替:

(function ($) {
  ...
})(jQuery);

also didn't work.也没有工作。

So I deleted all my code:所以我删除了所有代码:

(function ($) {
  $('.nav-item').on('click', function() {
    $(this).toggleClass('open');
    $(this).siblings().removeClass('open');
  });

  $(window).resize(function() {
    if($(window).width() < 1200) {
      $('.nav-item').on('click', function() {
        $(this).toggleClass('open');
        $(this).siblings().removeClass('open');
      });
    }
  });
})(jQuery);

and I added this code:我添加了这段代码:

(function ($) {
Drupal.behaviors.navclick = {
    attach: function (context, settings) {
      $('.nav-item', context).click(function () {
        $(this).toggleClass('open').siblings().removeClass('open');
      });
    }
  };
})(jQuery);

And now it works, and on resizing the window, and after just firing the page (previously it didn't work on just firing the page).现在它可以工作了,在调整 window 的大小时,在触发页面之后(以前它在触发页面时不起作用)。 Why?为什么? I don't know.我不知道。

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

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