[英]Having trouble with jQuery toggle function for dropdown menu
I have a dropdown menu that slides up and down when toggled to reveal it's menu items. 我有一个下拉菜单,当切换以显示其菜单项时,它会上下滑动。 The problem is that when i go to click one of the menu items after the menu has slid down, it is recognized as a toggle and the menu slides up without opening the link of the menu item. 问题是,当我在菜单向下滑动后单击某个菜单项时,它会被识别为切换按钮,并且菜单向上滑动而不打开菜单项的链接。
HTML 的HTML
<li class="dropdown">
<a href="#">Carbohydrates, proteins & fats</a>
<ul>
<li><a href="carbohydrates.php">Carbohydrates</a></li>
<li><a href="proteins.php">Proteins</a></li>
<li><a href="fats.php">Fats</a></li>
</ul>
</li>
dropdown script: 下拉脚本:
$(document).ready(function () {
$('.dropdown').toggle(
function () {
//show its submenu
$('ul', this).slideDown(300);
},
function () {
//hide its submenu
$('ul', this).slideUp(300);
}
);
});
I'd appreciate any help with this. 我将不胜感激。
Why are you individually closing/opening the menu items? 为什么要分别关闭/打开菜单项? Everything gets hidden anyway when the parent <li>
element is hidden -- why do anything after that? 父<li>
元素被隐藏后,所有内容都会被隐藏起来-为什么之后还要做什么?
I think you want to disable event bubbling . 我想你想禁用事件冒泡 。
EDIT 1: You're over-complicating things. 编辑1:您太复杂了。 Try this: 尝试这个:
$(document).ready(function () {
$('.dropdown').toggle();
});
EDIT 2: Can you be more specific about what you want? 编辑2:您可以更具体地想要什么吗? If you want a particular element, when clicked, to control the motion of a particular list, try this: 如果要单击某个特定元素来控制特定列表的运动,请尝试以下操作:
$(document).ready(function () {
$('a.someLink').click(function() {
$('.dropdown').toggle();
});
});
Try moving the triggering link outside of the dropdown items 尝试将触发链接移至下拉项之外
<a href="#" class="dropdown">Carbohydrates, proteins & fats</a>
<div class="divdropdown">
<ul>
<li><a href="carbohydrates.php">Carbohydrates</a></li>
<li><a href="proteins.php">Proteins</a></li>
<li><a href="fats.php">Fats</a></li>
</ul>
</div>
And then slightly modify your jquery 然后稍微修改你的jQuery
$(document).ready(function () {
$('.dropdown').toggle(
function () {
//show its submenu
$('ul', $(".divdropdown")).slideDown(300);
},
function () {
//hide its submenu
$('ul', $(".divdropdown")).slideUp(300);
}
);
});
Unfortunately none of the above answers were working for me. 不幸的是,以上答案都对我没有帮助。 I found a solution shortly afterwards. 之后不久,我找到了解决方案。
HTML 的HTML
<li class="dropdown">
<a class="disablelink" href="#">Carbohydrates, proteins & fats</a>
<ul class="sub_navigation">
<li><a href="carbohydrates.php">Carbohydrates</a></li>
<li><a href="proteins.php">Proteins</a></li>
<li><a href="fats.php">Fats</a></li>
</ul>
</li>
jquery script jQuery脚本
$(document).ready(function() {
$('.dropdown').click(function() {
// When the event is triggered, grab the current element 'this' and
// find it's children '.sub_navigation' and display/hide them
$(this).find('.sub_navigation').slideToggle();
});
$(".disablelink").click(function(e) {
e.preventDefault();
});
});
That solution is working perfectly for me. 该解决方案非常适合我。 I added in e.preventDefault();
我添加了e.preventDefault();
to stop the page jumping up and down when i clicked on the link. 当我单击链接时,停止页面上下跳动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.