开发者

add / remove class on span inside a tag

开发者 https://www.devze.com 2022-12-14 21:45 出处:网络
I\'ve tried various options but can\'t get the silly thing to work. How do I get the span inside an < a > tag within an < li > to change class to \"active\"; then remove it when another开发者_C百

I've tried various options but can't get the silly thing to work. How do I get the span inside an < a > tag within an < li > to change class to "active"; then remove it when another开发者_C百科 < a > is clicked?

<ul id="dumb">
<li><a href="#">Something<span></span></a></li>
<li><a href="#">Something Else<span></span></a></li>
</ul>

Clicking the < a > should give the span a class of "active" and when another is clicked, it should remove it from the original and add it to the span of that < a >...

Thanks!


$(function(){
      $("#dumb > li > a").click ( function(){
      $("#dumb > li > a > span").removeClass ( 'active' );
      $(this).find('span').addClass('active');
      return false;
     });
});


Try this:

$(document).ready(function(){
    var $MySpans = $("#dumb li a span");
    $MySpans.click(function(){
        $MySpans.removeClass();
        $(this).addClass("active");
    });
});

You might try this as well as it will be a faster selector if it works:

var $MySpans = $("#dumb>li>a>span");


jQuery('#dumb li a span').addClass( "active" );

0

精彩评论

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