开发者

Change CSS style on hyperlink click using jQuery

开发者 https://www.devze.com 2023-03-02 20:38 出处:网络
I want to change the style on anchor tags using jquery, once the link is clicked. <tr class=\"row-tab\" style=\"height: 30px;\">

I want to change the style on anchor tags using jquery, once the link is clicked.

<tr class="row-tab" style="height: 30px;">
    <td class="subtab-selected"><a href="#" id="as1">TOP % GAINERS</a></td开发者_如何学Go>
    <td class="subtab-notselected"><a href="#" id="as2">TOP % LOSERS</a></td>
</tr>

$('#as1, #as2').click(function(){
    $('#as1, #as2').parent().removeClass('subtab-selected').addClass('subtab-notselected');

    $('this').parent().addClass('subtab-selected');

    return false;                  
});

I'm trying to change the CSS class when a link is clicked but the second line of the jquery code does not get executed.

Can someone tell me what I am doing wrong?


Try removing the quotes around 'this'


I wrote a solution without the fixed #ids on the jQuery:

$("td a").click(function() {
    var p = $(this).parents("tr");
    $("td", p).removeClass("subtab-selected").addClass("subtab-notselected");
    $(this).parent().addClass("subtab-selected");
    return false;
});

You can see it runing here.


Try changing that second line to:

$('this').parent().addClass('subtab-selected').removeClass('subtab-notselected');

I assume you wouldn't want both.

0

精彩评论

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

关注公众号