专业菜单使用JavaScript和jQuery(最后部分)

huangapple go评论64阅读模式
英文:

Professional menu with JavaScript and jQuery (The last part)

问题

在我的以前的问题中,借助Michael M.的帮助,我解决了我的网站菜单的所有不足之处。

但在将菜单转移到我的网站后,我意识到我的菜单不打开任何链接,所有菜单和子菜单选项都只处于显示模式。

我从与删除菜单下的链接相关的脚本中删除了以下代码,但它仍然没有帮助:

sub_menu.stopPropagation();

以下是您提供的代码的翻译。如果您需要更多的帮助,请告诉我:

let icon = document.querySelector(".icon_menu");
let nav = document.querySelector(".main_menu");

$('.back').hide();
$('.back').click(function() {
  if ($(this).is(':hidden')) return;
  $(this).toggle();

  icon.classList = "bi bi-grid-fill icon_menu";
  icon.style.left = "2%";
  icon.style.color = "#a66fff";
  icon.style.fontSize = "40px";

  nav.style.left = '-300px';
});

icon.addEventListener("click", function() {
  if (this.classList.contains("bi-grid-fill")) {
    this.classList = "bi bi-x-circle-fill icon_menu";
    icon.style.left = "21%";
    icon.style.color = "#ff6f6f";
    icon.style.fontSize = "30px";

    nav.style.left = 0;
  } else {
    this.classList = "bi bi-grid-fill icon_menu";
    icon.style left = "2%";
    icon.style color = "#a66fff";
    icon.style fontSize = "40px";

    nav.style.left = "-300px";
  }
  $('.back').toggle();
});

$('.main_menu li ul').each(function() {
  $(this).parent().addClass('submenu');
});

$('.submenu').click(function(event) {
  if ($(event.target).is($(this).find('ul a'))) { return; }
  
  event.preventDefault();
  $(this).children('ul').slideToggle();
  $(this).toggleClass('open');
});

/* CSS 样式部分不需要翻译,保持原样 */

/* HTML 部分不需要翻译,保持原样 */

你感谢了编号为Steve(76484)的人完成了你的代码。

这段代码在HTML模式下完全正常,但当我将这段代码添加到WordPress时,出现了一个新问题。

当我将菜单引入WordPress时,只能添加一个子菜单。添加第二个子菜单并单击它后,所有子菜单将关闭,不会显示任何子菜单。

我已经将菜单代码转化为WordPress代码:

<section>
  <nav id="nav">
    <span class="fas fa-bars icon_menu"></span>
    <aside class="main_menu">
      <?php
      if (has_nav_menu('top_menu')) {
        wp_nav_menu(array(
          'theme_location' => 'top_menu',
          'menu_class' => 'menu',
          'container' => false
        ));
      }
      ?>
    </aside>
  </nav>
</section>
<div class="back"></div>

我添加到模板function文件的代码如下:

if (!function_exists('top_navigation_menus')) {
  function top_navigation_menus() {
    $locations = array(
      'top_menu' => __('站点菜单', 'text_domain'),
    );
    register_nav_menus($locations);
  }
  add_action('init', 'top_navigation_menus');
}

我没有更改样式,它们与上面的样式相同。

英文:

In my previous questions and with the help of Michael M., I fixed all the shortcomings of my site's menu.

But after transferring the menu to my site, I realized that my menu does not open any links and all menu and sub-menu options are only in display mode.

I deleted the following code from the scripts, which was related to deleting the link under the menu, but it still didn't help:

   sub_menu.stopPropagation();

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let icon = document.querySelector(&quot;.icon_menu&quot;);
let nav = document.querySelector(&quot;.main_menu&quot;);
$(&#39;.back&#39;).hide();
$(&#39;.back&#39;).click(function() {
if ($(this).is(&#39;:hidden&#39;)) return;
$(this).toggle();
icon.classList = &quot;bi bi-grid-fill icon_menu&quot;;
icon.style.left = &quot;2%&quot;;
icon.style.color = &quot;#a66fff&quot;;
icon.style.fontSize = &quot;40px&quot;;
nav.style.left = &#39;-300px&#39;;
});
icon.addEventListener(&quot;click&quot;, function() {
if (this.classList.contains(&quot;bi-grid-fill&quot;)) {
this.classList = &quot;bi bi-x-circle-fill icon_menu&quot;;
icon.style.left = &quot;21%&quot;;
icon.style.color = &quot;#ff6f6f&quot;;
icon.style.fontSize = &quot;30px&quot;;
nav.style.left = 0;
} else {
this.classList = &quot;bi bi-grid-fill icon_menu&quot;;
icon.style.left = &quot;2%&quot;;
icon.style.color = &quot;#a66fff&quot;;
icon.style.fontSize = &quot;40px&quot;;
nav.style.left = &quot;-300px&quot;;
}
$(&#39;.back&#39;).toggle();
});
$(&#39;.main_menu li ul&#39;).each(function() {
$(this).parent().addClass(&#39;submenu&#39;)
});
$(&#39;.submenu&#39;).click(function(event) {
if ($(event.target).is($(this).find(&#39;ul a&#39;))) { return; }
event.preventDefault();
$(this).children(&#39;ul&#39;).slideToggle();
$(this).toggleClass(&#39;open&#39;);
});

<!-- language: lang-css -->

.main_menu {
position: absolute;
top: 0;
left: -300px;
bottom: 0;
z-index: 999;
background: #eee;
padding-right: 2rem;
transition: all 1s ease;
}
.icon_menu {
position: fixed;
top: 10%;
left: 2%;
font-size: 40px;
color: #a66fff;
cursor: pointer;
z-index: 99999;
transition: all 1.1s ease;
}
.main_menu ul {
list-style: none;
line-height: 60px;
font-size: 35px;
}
.main_menu ul li a {
color: #000000;
text-decoration: none;
padding-right: 15px;
padding-left: 40px;
margin-left: -60px;
margin-bottom: 10px;
}
.back {
width: 100%;
height: 100%;
position: fixed;
z-index: 9 !important;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #00000056;
}
.main_menu ul li ul {
display: none;
}
.main_menu .submenu&gt;a::after {
content: &#39; → &#39;;
}
.main_menu .open&gt;a::after {
content: &#39; ↓ &#39; !important;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;section&gt;
&lt;nav id=&quot;nav&quot;&gt;
&lt;span class=&quot;bi bi-grid-fill icon_menu&quot; style=&quot;right: 46%; color: rgb(255, 255, 255); font-size: 80px;&quot;&gt;&lt;/span&gt;
&lt;aside class=&quot;main_menu&quot; style=&quot;right: 0px;&quot;&gt;
&lt;ul id=&quot;menu-menu-2&quot; class=&quot;menu&quot;&gt;
&lt;li class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-48&quot;&gt;&lt;a href=&quot;#&quot;&gt;&lt;span
class=&quot;dashicons dashicons-admin-home after-menu-image-icons&quot;&gt;&lt;/span&gt;&lt;span
class=&quot;menu-image-title-after menu-image-title&quot;&gt;home&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-49 submenu&quot;&gt;
&lt;a href=&quot;#&quot;&gt;&lt;span class=&quot;dashicons dashicons-welcome-learn-more after-menu-image-icons&quot;&gt;&lt;/span&gt;&lt;span
class=&quot;menu-image-title-after menu-image-title&quot;&gt;Test text&lt;/span&gt;&lt;/a&gt;
&lt;ul class=&quot;sub-menu&quot;&gt;
&lt;li class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-78&quot;&gt;&lt;a href=&quot;#&quot;
class=&quot;menu-image-title-after menu-image-not-hovered&quot;&gt;&lt;img
src=&quot;https://localhost/site/wp-content/uploads/2023/01/Music-114.ico&quot;
class=&quot;menu-image menu-image-title-after&quot; alt=&quot;&quot; decoding=&quot;async&quot; loading=&quot;lazy&quot; width=&quot;36&quot;
height=&quot;36&quot;&gt;&lt;span class=&quot;menu-image-title-after menu-image-title&quot;&gt;Test text&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-82&quot;&gt;&lt;a href=&quot;#&quot;
class=&quot;menu-image-title-after menu-image-not-hovered&quot;&gt;&lt;img
src=&quot;https://localhost/site/wp-content/uploads/2023/01/Hardware-Devices-316.ico&quot;
class=&quot;menu-image menu-image-title-after&quot; alt=&quot;&quot; decoding=&quot;async&quot; loading=&quot;lazy&quot; width=&quot;16&quot;
height=&quot;16&quot;&gt;&lt;span class=&quot;menu-image-title-after menu-image-title&quot;&gt;Test text&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li
class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-50 submenu&quot;&gt;
&lt;a href=&quot;#&quot; class=&quot;menu-image-title-after menu-image-not-hovered&quot;&gt;&lt;img
src=&quot;https://localhost/site/wp-content/uploads/2023/01/Music-148.ico&quot;
class=&quot;menu-image menu-image-title-after&quot; alt=&quot;&quot; decoding=&quot;async&quot; loading=&quot;lazy&quot; width=&quot;36&quot;
height=&quot;36&quot;&gt;&lt;span class=&quot;menu-image-title-after menu-image-title&quot;&gt;Test text&lt;/span&gt;&lt;/a&gt;
&lt;ul class=&quot;sub-menu&quot;&gt;
&lt;li class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-77&quot;&gt;&lt;a href=&quot;#&quot;
class=&quot;menu-image-title-after menu-image-not-hovered&quot;&gt;&lt;img
src=&quot;https://localhost/site/wp-content/uploads/2023/01/button-yellow.ico&quot;
class=&quot;menu-image menu-image-title-after&quot; alt=&quot;&quot; decoding=&quot;async&quot; loading=&quot;lazy&quot; width=&quot;16&quot;
height=&quot;16&quot;&gt;&lt;span class=&quot;menu-image-title-after menu-image-title&quot;&gt;Test text&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-169&quot;&gt;&lt;a href=&quot;#&quot;&gt;Test
text&lt;/a&gt;&lt;/li&gt;
&lt;li
class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-170 submenu&quot;&gt;
&lt;a href=&quot;#&quot;&gt;Test text&lt;/a&gt;
&lt;ul class=&quot;sub-menu&quot;&gt;
&lt;li class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-80&quot;&gt;&lt;a href=&quot;#&quot;
class=&quot;menu-image-title-after menu-image-not-hovered&quot;&gt;&lt;img
src=&quot;https://localhost/site/wp-content/uploads/2023/01/Star-27.ico&quot;
class=&quot;menu-image menu-image-title-after&quot; alt=&quot;&quot; decoding=&quot;async&quot; loading=&quot;lazy&quot; width=&quot;36&quot;
height=&quot;36&quot;&gt;&lt;span class=&quot;menu-image-title-after menu-image-title&quot;&gt;Test text&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-168&quot;&gt;&lt;a href=&quot;#&quot;&gt;Test
text&lt;/a&gt;&lt;/li&gt;
&lt;li
class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-167 submenu&quot;&gt;
&lt;a href=&quot;#&quot;&gt;Test text&lt;/a&gt;
&lt;ul class=&quot;sub-menu&quot;&gt;
&lt;li
class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-166 submenu&quot;&gt;
&lt;a href=&quot;#&quot;&gt;Test text&lt;/a&gt;
&lt;ul class=&quot;sub-menu&quot;&gt;
&lt;li
class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-52 submenu&quot;&gt;
&lt;a href=&quot;#&quot; class=&quot;menu-image-title-after menu-image-not-hovered&quot;&gt;&lt;img
src=&quot;https://localhost/site/wp-content/uploads/2023/01/Hardware-Devices-316.ico&quot;
class=&quot;menu-image menu-image-title-after&quot; alt=&quot;&quot; decoding=&quot;async&quot; loading=&quot;lazy&quot;
width=&quot;16&quot; height=&quot;16&quot;&gt;&lt;span class=&quot;menu-image-title-after menu-image-title&quot;&gt;Test
text&lt;/span&gt;&lt;/a&gt;
&lt;ul class=&quot;sub-menu&quot;&gt;
&lt;li class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-79&quot;&gt;&lt;a
href=&quot;#&quot; class=&quot;menu-image-title-after menu-image-not-hovered&quot;&gt;&lt;img
src=&quot;https://localhost/site/wp-content/uploads/2023/01/Firefox-4.ico&quot;
class=&quot;menu-image menu-image-title-after&quot; alt=&quot;&quot; decoding=&quot;async&quot; loading=&quot;lazy&quot;
width=&quot;36&quot; height=&quot;36&quot;&gt;&lt;span class=&quot;menu-image-title-after menu-image-title&quot;&gt;Test
text&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-81&quot;&gt;&lt;a
href=&quot;#&quot; class=&quot;menu-image-title-after menu-image-not-hovered&quot;&gt;&lt;img
src=&quot;https://localhost/site/wp-content/uploads/2023/01/Music-161.ico&quot;
class=&quot;menu-image menu-image-title-after&quot; alt=&quot;&quot; decoding=&quot;async&quot; loading=&quot;lazy&quot;
width=&quot;36&quot; height=&quot;36&quot;&gt;&lt;span class=&quot;menu-image-title-after menu-image-title&quot;&gt;Test
text&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;menu-item menu-item-type-custom menu-item-object-custom menu-item-163&quot;&gt;&lt;a
href=&quot;#&quot;&gt;Test text&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/aside&gt;
&lt;/nav&gt;
&lt;/section&gt;

<!-- end snippet -->

Many thanks to Steve(76484) for completing my code.

This code works perfectly fine in HTML mode, but when I add this code to WordPress, a new problem occurs.

When I introduce the menu to WordPress, only one sub-menu can be added to it. By adding the second sub-menu and clicking on it, all the sub-menus will be closed and no sub-menu will be displayed.

The menu code that I converted to WordPress code:

 &lt;section&gt;
&lt;nav id=&quot;nav&quot;&gt;
&lt;span class=&quot;fas fa-bars icon_menu&quot;&gt;&lt;/span&gt;
&lt;aside class=&quot;main_menu&quot;&gt;
&lt;?php
if ( has_nav_menu( &#39;top_menu&#39; ) ) {
wp_nav_menu( array(
&#39;theme_location&#39; =&gt; &#39;top_menu&#39;,
&#39;menu_class&#39; =&gt; &#39;menu&#39;,
&#39;container&#39; =&gt; false
) );
}
?&gt;
&lt;/aside&gt;
&lt;/nav&gt;
&lt;/section&gt;
&lt;div class=&quot;back&quot;&gt;&lt;/div&gt;

The code I added to my template function file:

if ( !function_exists( &#39;top_navigation_menus&#39; ) ) {
function top_navigation_menus() {
$locations = array(
&#39;top_menu&#39; =&gt; __( &#39;Site menu&#39;, &#39;text_domain&#39; ),
);
register_nav_menus( $locations );
}
add_action( &#39;init&#39;, &#39;top_navigation_menus&#39; );
}

I did not change the styles and they are the same as the styles above.

答案1

得分: 2

我不确定你是否需要在你的.submenu点击处理程序中使用.stopPropagation().preventDefault()调用。

我建议在该处理程序中添加一个检查,检查点击的target是否是<ul>内的<a>,如果是,则不执行函数体的其余部分,也就是不打开/关闭子菜单。

更新

为了防止li.submenu > a的点击跟随href,你需要重新添加event.preventDefault()。下面的代码片段已经更新。

let icon = document.querySelector(".icon_menu");
let nav = document.querySelector(".main_menu");

$('.back').hide();
$('.back').click(function() {
  if ($(this).is(':hidden')) return;
  $(this).toggle();

  icon.classList = "bi bi-grid-fill icon_menu";
  icon.style.left = "2%";
  icon.style.color = "#a66fff";
  icon.style.fontSize = "40px";

  nav.style.left = '-300px';
});

icon.addEventListener("click", function() {
  if (this.classList.contains("bi-grid-fill")) {
    this.classList = "bi bi-x-circle-fill icon_menu";
    icon.style.left = "21%";
    icon.style.color = "#ff6f6f";
    icon.style.fontSize = "30px";

    nav.style.left = 0;
  } else {
    this.classList = "bi bi-grid-fill icon_menu";
    icon.style left = "2%";
    icon.style color = "#a66fff";
    icon.style fontSize = "40px";

    nav.style left = "-300px";
  }
  $('.back').toggle();
});

$('.main_menu li ul').each(function() {
  $(this).parent().addClass('submenu')
});

$('.submenu').click(function(event) {
  if ($(event.target).is($(this).find('ul a'))) { return; }
  
  event.preventDefault();
  $(this).children('ul').slideToggle();
  $(this).toggleClass('open');
});

请注意,这是关于如何更新你的代码以包括event.preventDefault()的说明。

另一个更新

我们可以通过明确地定位每个li.submenu中的第一个<a>来简化所有这些。我们可以用以下代码替换.submenu的点击处理程序:

$('li.submenu > a').click(function(event) {
  const $submenu = $(this).parent();
  
  $submenu.children('ul').slideToggle();
  $submenu.toggleClass('open');
  event.preventDefault();
});

这里可以看到示例fiddle。

英文:

I am not sure that you need the .stopPropagation() or .preventDefault() calls in your .submenu click handler.

I would just add a guard to that handler that checks whether the target of the click was an &lt;a&gt; within the &lt;ul&gt;, and if so, do not execute the rest of the function body - the opening/closing of the submenu.

Update

To prevent the click of the li.submenu &gt; a from following the href you would need to add the event.preventDefault() back. The snippet below has been updated.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let icon = document.querySelector(&quot;.icon_menu&quot;);
let nav = document.querySelector(&quot;.main_menu&quot;);
$(&#39;.back&#39;).hide();
$(&#39;.back&#39;).click(function() {
if ($(this).is(&#39;:hidden&#39;)) return;
$(this).toggle();
icon.classList = &quot;bi bi-grid-fill icon_menu&quot;;
icon.style.left = &quot;2%&quot;;
icon.style.color = &quot;#a66fff&quot;;
icon.style.fontSize = &quot;40px&quot;;
nav.style.left = &#39;-300px&#39;;
});
icon.addEventListener(&quot;click&quot;, function() {
if (this.classList.contains(&quot;bi-grid-fill&quot;)) {
this.classList = &quot;bi bi-x-circle-fill icon_menu&quot;;
icon.style.left = &quot;21%&quot;;
icon.style.color = &quot;#ff6f6f&quot;;
icon.style.fontSize = &quot;30px&quot;;
nav.style.left = 0;
} else {
this.classList = &quot;bi bi-grid-fill icon_menu&quot;;
icon.style.left = &quot;2%&quot;;
icon.style.color = &quot;#a66fff&quot;;
icon.style.fontSize = &quot;40px&quot;;
nav.style.left = &quot;-300px&quot;;
}
$(&#39;.back&#39;).toggle();
});
$(&#39;.main_menu li ul&#39;).each(function() {
$(this).parent().addClass(&#39;submenu&#39;)
});
$(&#39;.submenu&#39;).click(function(event) {
if ($(event.target).is($(this).find(&#39;ul a&#39;))) { return; }
event.preventDefault();
$(this).children(&#39;ul&#39;).slideToggle();
$(this).toggleClass(&#39;open&#39;);
});

<!-- language: lang-css -->

.main_menu {
position: absolute;
top: 0;
left: -300px;
bottom: 0;
z-index: 999;
background: #eee;
padding-right: 2rem;
transition: all 1s ease;
}
.icon_menu {
position: fixed;
top: 10%;
left: 2%;
font-size: 40px;
color: #a66fff;
cursor: pointer;
z-index: 99999;
transition: all 1.1s ease;
}
.main_menu ul {
list-style: none;
line-height: 60px;
font-size: 35px;
}
.main_menu ul li a {
color: #000000;
text-decoration: none;
padding-right: 15px;
padding-left: 40px;
margin-left: -60px;
margin-bottom: 10px;
}
.back {
width: 100%;
height: 100%;
position: fixed;
z-index: 9 !important;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #00000056;
}
.main_menu ul li ul {
display: none;
}
.main_menu .submenu&gt;a::after {
content: &#39; → &#39;;
}
.main_menu .open&gt;a::after {
content: &#39; ↓ &#39; !important;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;section&gt;
&lt;nav id=&quot;nav&quot;&gt;
&lt;span class=&quot;bi bi-grid-fill icon_menu&quot;&gt;&lt;/span&gt;
&lt;aside class=&quot;main_menu&quot;&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;home&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;our articles&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;social&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;Academic&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;historical&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#&quot;&gt;about us&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/aside&gt;
&lt;/nav&gt;
&lt;/section&gt;
&lt;div class=&quot;back&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

Another Update

We can simplify all of this by explicitly targeting the first &lt;a&gt; within each li.submenu. We replace the .submenu click handler with:

$(&#39;li.submenu &gt; a&#39;).click(function(event) {
const $submenu = $(this).parent();
$submenu.children(&#39;ul&#39;).slideToggle();
$submenu.toggleClass(&#39;open&#39;);
event.preventDefault();
});

See example fiddle here.

huangapple
  • 本文由 发表于 2023年2月26日 22:43:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定