简体   繁体   English

更改ul的样式onclick - css / jquery

[英]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');
});

JS Fiddle demo . JS小提琴演示

References: 参考文献:

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

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM