[英]Change ul's style onclick - css/jquery
I have this simple menu with some <ul>
s that loads its content onclicking on it. 我有这个简单的菜单,其中包含一些
<ul>
,可以点击它来加载其内容。 And I want to change the style of every ul on loading the content that belongs to it. 我想在加载属于它的内容时改变每个ul的样式。 Here's the menu: http://jsfiddle.net/EPvGf/11/
这是菜单: http : //jsfiddle.net/EPvGf/11/
I'd suggest: 我建议:
$(this).closest('ul').addClass('newStyle');
Though within a click-handler (assuming nested ul
elements): 虽然在点击处理程序中(假设嵌套的
ul
元素):
$('li').click(function(e){
$(e.target).closest('ul').addClass('newStyle');
});
References: 参考文献:
addClass()
. addClass()
。 click()
. click()
。 closest()
. closest()
。 event.target
. event.target
。 I think you meant styling the corresponding parent li
(with the border bottom using the class current
) as you are doing for the first one by default. 我认为你的意思是设置相应的父
li
(边框底部使用类current
),就像你默认为第一个做的那样。 You can try this: 你可以试试这个:
$('.current').not(
$(this)
.closest('li')
.addClass('current'))
.removeClass('current');
or just 要不就
$(this)
.closest('li') //Get to the parent li
.addClass('current') //add the class current
.siblings('.current') // select the sibling with the same class (previous selection)
.removeClass('current'); //remove the class from there
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.