我希望我的菜单在点击时显示和隐藏列表项,默认情况下它们是隐藏的。问题是我的菜单是在管理部分生成的,所以如果我在菜单选项中将特定链接的URL字段设置为null,它会自动为其分配网址的网址。所以当我点击它重新加载主页。
我想要的是,当我点击任何父li时,它应该停止生成默认事件,因此使用防止默认。但在我的情况下,它不起作用,并在显示父母的列表项后重新加载页面。
这是一个小提琴 Fiddle
HTML
<div class="mega-col col-xs-12 col-sm-12 col-md-4" data-type="menu">
<div class="mega-col-inner">
<ul>
<li class="parent dropdown-submenu mega-group"><a class="dropdown-toggle" data-toggle="dropdown" href=""><span class="menu-title">Massachusetts Stores</span><b class="caret"></b></a>
<div class="dropdown-mega level2">
<div class="dropdown-menu-inner">
<div class="row">
<div class="col-sm-12 mega-col" data-colwidth="12" data-type="menu">
<div class="mega-col-inner">
<ul>
<li class=" "><a href="index.php?route=common/location_details&loc_id=24"><span class="menu-title">Burlington Mall, MA</span></a>
</li>
<li class=" "><a href="index.php?route=common/location_details&loc_id=25"><span class="menu-title">Burlington Mall, MA - Cart</span></a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</li>
<li class="parent dropdown-submenu mega-group"><a class="dropdown-toggle" data-toggle="dropdown" href=""><span class="menu-title">New Jersey Stores</span><b class="caret"></b></a>
<div class="dropdown-mega level2">
<div class="dropdown-menu-inner">
<div class="row">
<div class="col-sm-12 mega-col" data-colwidth="12" data-type="menu">
<div class="mega-col-inner">
<ul>
<li class=" "><a href="index.php?route=common/location_details&loc_id=26"><span class="menu-title">Brunswick Square Mall, NJ</span></a>
</li>
<li class=" "><a href="index.php?route=common/location_details&loc_id=30"><span class="menu-title">Garden State Plaza, NJ</span></a>
</li>
<li class=" "><a href="index.php?route=common/location_details&loc_id=27"><span class="menu-title">Menlo Park Mall, NJ</span></a>
</li>
<li class=" "><a href="index.php?route=common/location_details&loc_id=29"><span class="menu-title">Ocean County Mall, NJ</span></a>
</li>
<li class=" "><a href=""><span class="menu-title">Rockaway Townsquare, NJ</span></a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</li>
<li class="parent dropdown-submenu mega-group"><a class="dropdown-toggle" data-toggle="dropdown" href=""><span class="menu-title">New York Stores</span><b class="caret"></b></a>
<div class="dropdown-mega level2">
<div class="dropdown-menu-inner">
<div class="row">
<div class="col-sm-12 mega-col" data-colwidth="12" data-type="menu">
<div class="mega-col-inner">
<ul>
<li class=" "><a href="index.php?route=common/location_details&loc_id=31"><span class="menu-title">Galleria at White Plains, NY</span></a>
</li>
<li class=" "><a href="index.php?route=common/location_details&loc_id=42"><span class="menu-title">Manhattan, NY-Toys 'R' Us </span></a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</li>
<li class="parent dropdown-submenu mega-group"><a class="dropdown-toggle" data-toggle="dropdown" href=""><span class="menu-title">North Carolina Stores</span><b class="caret"></b></a>
<div class="dropdown-mega level2">
<div class="dropdown-menu-inner">
<div class="row">
<div class="col-sm-12 mega-col" data-colwidth="12" data-type="menu">
<div class="mega-col-inner">
<ul>
<li class=" "><a href="index.php?route=common/location_details&loc_id=32"><span class="menu-title">CrabTree Valley, NC</span></a>
</li>
<li class=" "><a href="index.php?route=common/location_details&loc_id=2"><span class="menu-title">Fayetteville, NC</span></a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
JS:
$("#li_menu169 li.parent.dropdown-submenu.mega-group").click(function(event) {
var id = $(this).prop('id');
if (id == 'yes') {
//i want to prevent
} else {
event.preventDefault();
$(this).children('.dropdown-mega.level2').show();
$(this).children('.dropdown-mega.level2').hide();
//redirect
}
});
Css
li.parent.dropdown-submenu.mega-group .dropdown-mega.level2 {
display: none;
}
li
{
padding:10px;
position: relative;
margin:auto;
}
答案 0 :(得分:2)
以下是一个工作示例:http://jsfiddle.net/hk1w89zw/
不确定您试图通过id prop
实现的目标。通常,最好在链接(onClick
- 代码)而不是<a>
上设置li
事件。并使用以下jQuery代码:
$("li.parent.dropdown-submenu.mega-group > a").on('click', function(event) {
event.preventDefault();
$('.dropdown-mega.level2').hide();
$(this).closest('.parent').find('.dropdown-mega.level2').show();
});
答案 1 :(得分:0)
也许你想要这样的东西
$("#li_menu169 li.parent.dropdown-submenu.mega-group").click(function(event) {
var id = $(this).prop('id');
if (id == 'yes') {
//i want to prevent
} else {
$(this).children('.dropdown-mega.level2').show();
$(this).children('.dropdown-mega.level2').hide();
//redirect
window.location.href="location_of_the_filehere";
}
event.preventDefault();
});
答案 2 :(得分:0)
我必须承认我的代码错了。
这是https://jsbin.com/gigowopijo/3/edit?html,js,console,output(JSBin)。
首先,您没有ID为“#li_menu169”的列表。将其添加到ul元素
<div class="mega-col col-xs-12 col-sm-12 col-md-4" data-type="menu"><div class="mega-col-inner">
<ul id="li_menu169">
稍后你会显示和隐藏相同的元素,这有点令人困惑。因此,我建议首先隐藏所有内部元素,而不是显示“this”内容的内部元素。
$("#li_menu169 li.parent.dropdown-submenu.mega-group").click(function(event) {
var id = $(this).attr('id');
if (id === 'yes' && typeof id !== undefined) {
console.log("I'm there");
//i want to prevent
} else {
console.log("i'm here");
event.preventDefault();
$('.dropdown-mega.level2').hide();
$(this).find('.dropdown-mega.level2').show();
//redirect
}
});
在JSbin中它按预期工作。
答案 3 :(得分:0)
在触发输出时抓住event
$("#li_menu169 li.parent.dropdown-submenu.mega-group").click(function(event) {
event.preventDefault();
}