使用子菜单单击菜单项将在移动设备上打开所有其他子菜单

发布于 2025-01-31 13:04:09 字数 350 浏览 3 评论 0原文

在下面使用的jQuery函数添加功能以滑动菜单项的子菜单,该子菜单为移动视口提供了孩子。

        $('.submenu-open').click(function(e) {
        $('.menu-item-has-children li').slideToggle('slow');
    });

但是,它同时(在移动视口上)与孩子一起打开所有菜单项,但它只能打开该菜单项的子菜单。

这是 link

used below jquery function to add functionality to slide toggle the submenu of the menu items which has got children for the mobile viewport.

        $('.submenu-open').click(function(e) {
        $('.menu-item-has-children li').slideToggle('slow');
    });

But, it opens all the menu items with children at the same time (on mobile viewport), But it should only open the submenu of that menu item.

here is the link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じ违心 2025-02-07 13:04:09

虽然这可能不是确切的解决方案,但更多的是您需要的线路。

Vanilla JS,没有JQuery:

document.querySelectorAll('.submenu-open').addEventListener('click', function(event) {
    event.target.querySelector('.menu-item-has-children li').classList.toggle('slow');
})

您的问题是,您并不是只将类添加到单击元素中,而是所有这些元素。

While this may not be the exact solution, it is more on the line of what you need.

Vanilla JS, without JQuery:

document.querySelectorAll('.submenu-open').addEventListener('click', function(event) {
    event.target.querySelector('.menu-item-has-children li').classList.toggle('slow');
})

Your problem ist, that you aren't adding the class to only the clicked element, but all of them.

勿挽旧人 2025-02-07 13:04:09

在“单击事件”侦听器的回调函数中,您必须使用jQuery的$(this)对象首先关闭目标。发生。

然后使用方法 childror() 在该对象上以针对相关的对象无序列表.sub-menu

$('.submenu-open').click(function(e) {
        $(this).children('.sub-menu').slideToggle('slow');
    });
})

Within the click event listener's callback function, you have to use jQuery's $(this) object to first off target the very .menu-item-has-children where the click event occurred.

Then use the method children() on that object to target the related unordered list .sub-menu:

$('.submenu-open').click(function(e) {
        $(this).children('.sub-menu').slideToggle('slow');
    });
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文