[英]Get child <ul> in <ul> list without id and with e.preventDefault() on first level of <ul><li><a>
I have some menu with structure like this: 我有一些菜单结构如下:
<ul id="menu">
<li><a href="#"><span>First level link 1</span></a>
<ul>
<li><a href="#"><span>Child link 1</span></a></li>
<li><a href="#"><span>Child link 2</span></a></li>
</ul>
</li>
<li><a href="#"><span>First level link 2</span></a>
<ul>
<li><a href="#"><span>Child link 2</span></a></li>
<li><a href="#"><span>Child link 2</span></a></li>
</ul>
</li>
</ul>
It need to show only first level ul li, its ok, but then it needs to show by on click second level links. 它只需要显示第一级ul li,它的确定,但是它需要通过点击第二级链接来显示。 And i cant usw id or rel attr in ul or li - this is my problem to catch child .
而且我不能在ul或li中使用id或rel attr - 这是我抓住孩子的问题。
My code is like: 我的代码是这样的:
var menuRaw = $("ul#menu > li:lt(5)");
menuRaw.click(function(e){
e.preventDefault();
var menuItem = $(this);
menuRaw.removeClass("selected");
menuItem.addClass("selected");
$('#menu ul').hide();
menuItem.find("ul").show();
}
And it works but e.preventDefault() is blocking all links in menu (but on second level links needs to be working links as normally they do) 并且它有效,但是e.preventDefault()阻止了菜单中的所有链接(但是在二级链接上需要像通常那样工作链接)
Sorry for my English, and this is first question on stackoverflow. 对不起我的英文,这是关于stackoverflow的第一个问题。
You can select and process the clicks for each level like: 您可以选择并处理每个级别的点击次数,例如:
// first level links
$("ul#menu > li > a").click(function() {
e.preventDefault();
// show sub-menu etc..
}
// second level links (remove this block if you dont want any spl processing here)
$("ul#menu > li > ul > li > a").click(function() {
return true; // behave like normal links
});
You can achieve this by checking if the clicked li
has any ul
's nested within it. 你可以通过检查点击的
li
是否嵌套在其中的任何ul
来实现这一点。
$('#menu li').click(function(e) {
$this = $(this);
e.stopPropagation();
if ($this.has('ul').length) {
e.preventDefault();
$('ul', this).fadeToggle();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.