[英]adding an “active” class to parent and child element
Need to add an active class to both parent and child element if a user clicks on the child element in a list. 如果用户单击列表中的子元素,则需要向父元素和子元素添加活动类。 My html is as follows:-
我的HTML如下: -
<ul class="tabs">
<li class="accordion"><a href="#tab1">Bar</a>
<ul class="sub">
<li>lorem/li>
<li>ipsum</li>
<li>Lorem Ipsum</li>
<li>Dolor Sit Amet</li>
</ul>
</li>
and I'm using the jquery code below to add an active class to both the parent and child element yet I'm having errors:- 我正在使用下面的jquery代码为父元素和子元素添加一个活动类,但我有错误: -
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active").find(".sub li").removeClass("active");
$(this).addClass("active").find(".sub li").addClass("active");
});
Then styled my active class in CSS. 然后在CSS中设置我的活动类。 say example
比如说
.active {background:#EFEFEF;}
only the clicked (this) child element should have the active class applied to it and not the whole li child elements. 只有clicked(this)子元素应该应用活动类而不是整个li子元素。 Now together with the child li (say lorem) the parent li (bar) should also be highlighted.
现在和孩子李(说lorem)一起,也应该突出显示父母li(bar)。 Think of it like a tree accordion menu where both the selected child element and it's parent li have the active class with different css styling.
可以把它想象成树形手风琴菜单,其中所选子元素和它的父元素都具有不同css样式的活动类。
I'm just going to make an assumption here that you only want to add the active class to the list items like so: http://jsfiddle.net/gfkM4/ 我只想在这里假设您只想将活动类添加到列表项中,如下所示: http : //jsfiddle.net/gfkM4/
I hope that's what you were looking for. 我希望这就是你要找的东西。 Cheers.
干杯。
try this: 尝试这个:
$("ul.tabs li").click(function() {
$("ul.tabs li, .sub li").removeClass("active");
$(this).find(".sub li").andSelf().addClass("active");
})
Updated: http://jsfiddle.net/4G7TJ/3/ (per your new requirement that only one child is selected) 更新: http : //jsfiddle.net/4G7TJ/3/ (根据您的新要求,只选择了一个孩子)
$("ul.tabs li").click(function() {
// remove .active from all li descendants
$("ul.tabs li").not(this).removeClass("active");
$(this)
.addClass("active")
.not('.accordion')
.parents("li:first").addClass("active");
return false;
});
The idea behind returning false is to prevent the 'click' event from being propagated to the parent li
when a child li
is clicked, since that would undo the styling change on the child. 背后返回false的想法是为了防止“点击”事件被传播到父
li
一儿童在li
被点击,因为这将撤消对孩子的造型变化。
// only trigger the click on direct descendants
// (otherwise the kids get the event first and this won't work)
$("ul.tabs > li").click(function() {
// remove .active from all li descendants
$("ul.tabs li").not(this).removeClass("active");
$(this).addClass("active").find(".sub li").addClass("active");
});
Also - <li>lorem/li>
isn't closed, meaning that all of your tags are likely mismatched and you can't be sure what the 'current' list-item is (and in which list) when you click. 此外 -
<li>lorem/li>
未关闭,这意味着您的所有代码都可能不匹配,并且您无法确定单击时“当前”列表项(以及列表)中的内容。
Here is demo for applying active class on accordion menu option using jQuery. 这是使用jQuery在手风琴菜单选项上应用活动类的演示。
HTML: HTML:
<ul class="tabs">
<li class="accordion">
<a href="#tab1">
Bar
</a>
<ul class="sub">
<li>
lorem
</li>
<li>
ipsum
</li>
<li>
Lorem Ipsum
</li>
<li>
Dolor Sit Amet
</li>
</ul>
</li>
<li class="accordion">
<a href="#tab2">
Foo
</a>
<ul class="sub">
<li>
lorem
</li>
<li>
ipsum
</li>
<li>
Lorem Ipsum
</li>
<li>
Dolor Sit Amet
</li>
</ul>
</li>
</ul>
CSS: CSS:
.tabs li{
list-style:none;
}
.tabs li:hover{
background:#88ccf9;
}
.tabs li a{
color:#334499;
}
.tabs{
border:1px solid #3344ff;
background:#dcfcff;
padding:10px;
}
.tabs .accordion .sub{
padding:3px 3px 3px 18px;
display:none;
}
.tabs .active .sub{
display:block;
}
.tabs li.active{
background:#77d9c9;
}
JQuery: JQuery的:
$(function() {
$("ul.tabs li").click(function() {
// remove .active from all li descendants
$("ul.tabs li").not(this).removeClass("active");
//hide all sub menu except current active
$("ul.tabs li").not(this).find(".sub").hide(400);
//apply active class on current selected menu
$(this).addClass("active");
//check if sub menu exists
if($(this).find(".sub").length>0){
//show the selected sub menu
$(this).find(".sub").show(500);
//apply active class on all sub menu options
$(this).find(".sub li").andSelf().addClass("active");
}
});
});
I have done complete bin on http://codebins.com/bin/4ldqpa5 我在http://codebins.com/bin/4ldqpa5上完成了bin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.