jQuery:关闭打开的内容,打开关闭的内容
我正在开发一个简单的类似手风琴的垂直菜单:
单击“我们的解决方案”、“产品覆盖范围”、“我们的团队”或“联系我们”可打开或关闭子菜单。
但是,如果您连续单击其中两个或多个链接,所有子菜单都会打开。
我希望第一次单击任何菜单项即可打开其子菜单并关闭当前打开的子菜单。第二次单击应该只是关闭所单击链接的子菜单。
我将不胜感激专家的建议。
I am working on a simple accordion-like vertical menu:
Clicking on OUR SOLUTIONS, PRODUCT COVERAGE, OUR TEAM or CONTACT US opens or closes the submenus.
However, if you click on two or more of those links in a row, all the submenus will open.
I want the first click on any of the menu items to open its submenu and close those that are currently open. The second click should just close the submenu of the link being clicked.
I would be grateful for the expertly advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 jQuery :visible 选择器。在单击事件中,向上滑动已可见的那个,然后向下滑动要打开的那个。
编辑:这是我过去用来执行此操作的代码片段。注意我使用定义列表(dl、dt、dd)而不是无序列表(ul 和 li),但您可以调整代码:
希望这有帮助!
编辑:OP请求的实际代码:
解释:我使用了跨度,以便只有每个项目的文本才会触发手风琴行为。当单击一个跨度时,我们说 $(this).next() 来到达内部 ul。如果您不使用span并将单击处理程序附加到外部li,则在子菜单内单击将触发外部li的关闭。
Check out the jQuery :visible selector. On the click event, slide up whichever one is already visible and slide down the one you want to open.
EDIT: Here's a code snippet I used in the past to do this. Note I am using definition lists (dl, dt, dd) instead of your unordered lists (ul and li), but you can adapt the code:
Hope this helps!
EDIT: Actual code as requested by the OP:
Explanation: I used spans so that only the text of each item triggers the accordion behavior. When a span is clicked on, we say $(this).next() to get to the inner ul. If you don't use spans and you attach the click handler to the outer li, then clicks inside the submenu will trigger the closing of the outer li.