0

I have two level dropdown list: Home Categories Contact, etc.

Under the "Categories" there are sub categories.

I want to apply selected class to "Categories" whenever the user selects something from sub categories.

For example when user clicks "home" home is given class of selected but when user selects anything from subcategories under the "Categories" it should be given class of "selected". At present my jquery code is:

$(function() {
    $('a').click(function() {
        $('a').removeClass('selected');
        $(this).addClass('selected');  
    });
});

Which is adding selected class to dropdown items also.

Please help me out

4

2 回答 2

1

Looks like you are looking for the .parent() jQuery method. Simply give the subcategories a class of subcategories and when one of them are clicked, set the style on the $(this).parent()

$(function() {


    $('.cat').click(function() {

        $('.cat').removeClass('selected');
        $(this).addClass('selected');           

    });

    $('.subcat').click(function() {

        $('.cat').removeClass('selected');
        $(this).parent().addClass('selected');           

    });

});
于 2013-10-22T12:57:02.810 回答
0

You can use unique ids and same class names to all the subcategories.If any of it is clicked you can add the classess you require.

something like this

$(function() {
$('.subcategory').click(function() {
    $('.category').addClass('selected');


});
于 2013-10-22T12:54:33.730 回答