1
我已经基本上得到了这个标记:巢LI功能
<div class="navBar">
<div class="inner blueTheme">
<ul id="" class="navBarMenu">
<li class="xenonActiveMenu blue"><a href="/new_order">Orders</a>
<ul>
<li class="backBtn"><a href="#">< Back</a>
</li>
<li class="xenonActiveMenu "><a href="/new_order">New Order</a>
</li>
</ul>
</li>
<li class="green"><a href="/account_details">Profile</a>
<ul>
<li class="backBtn"><a href="#">Back</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
的Jquery:
$('.navBar .inner ul > li').bind('click', function(e){
e.preventDefault();
item = $(this);
if (item.attr('class') != 'selected' && item.attr('class') !='backBtn') {
item.addClass('selected');
var parent = item.parent();
var barWidth = item.width();
var hasSub = item.find('ul').length;
if (hasSub > 0){
//Item Does have a submenu
parent.children().each(function(){
$(this).animate({marginLeft: -barWidth}, 400);
});
}
} else {
var parent = $(this).parent().closest('li');
var barWidth = $(this).width();
parent.each(function(){
$(this).animate({marginLeft: barWidth}, 400);
});
}
});
我的问题是,它认为“礼”元素已经被点击了两次,监守有两家单独的嵌套LI元素的函数,我只是不知道它的方式,任何人都可以帮忙吗?
香
可能是您应该取消冒泡事件以避免该事件被运行两次。 if(event.stopPropagation){event.stopPropagation(); }(对于firefox和webkit)和event.cancelBubble = true; (用于IE) – MatRt
@ user1073122 jQuery将处理jQuery封装的事件对象上的['event.stopPropagation()'](http://api.jquery.com/event.stopPropagation/)的规范化。你应该提交这个答案。 –