开发者

jQuery change look af an active element?

开发者 https://www.devze.com 2023-02-05 09:04 出处:网络
I have ajax menu and I want to change the look of currently selected item. How to do that if the item is not a \"real\" link with href and just shows hidden div?

I have ajax menu and I want to change the look of currently selected item. How to do that if the item is not a "real" link with href and just shows hidden div?

Live example:

http://jsfiddle.net/9FXua/

When user clicks "Link #1" I want the text "Link #1" to b开发者_如何学Goe white and link's background to be black. When user clicks "Link #2" i want the text "Link #2" to be white (...).

Any ideas?


how about this? http://jsfiddle.net/9FXua/1/

$('a').click(function() {
    $(".active").removeClass("active");
    $(this).addClass("active");
    var id = $(this).attr('id');
    var divID = $('#'+id.substring(0,id.indexOf('-')));
    $('.hidden').hide();
    $(divID).show();       
});

with a new css class active

ul li a.active { background: #000; color: #fff; }


You can use:

$('.active').removeClass('active');
$(this).addClass('active');

in the click handler.

JS Fiddle demo.

0

精彩评论

暂无评论...
验证码 换一张
取 消