开发者

How do I find what class of anchor tag has using javascript/jquery?

开发者 https://www.devze.com 2023-03-09 02:59 出处:网络
I have HTML like this: <div id=\"signbtn\"> <a class=\"btnsignin\" href=\"signin.php\">Admin</a>

I have HTML like this:

<div id="signbtn">
    <a class="btnsignin" href="signin.php">Admin</a>
</div>

How do I find the class of the anchor tag embedd开发者_JAVA百科ed inside the div with id="signbtn".

I need this to be able to assign a different class to the anchor tag only if has "btnsignin" class.


You could use the .attr() function:

var class = $('#signbtn a').attr('class');

and to assign a different class to an anchor with the btnsignin class inside an element with id="signbtn":

$('#signbtn a.btnsignin').attr('class', 'some_new_class');


i think should do the trick for you

$('#signbtn').click(function() { 
    $.each($(this).children('a'), function(){
        if($(this).hasClass('btnsignin')){
            $(this).attr('class', 'anotherclass');
        }
    })
});

also you can change the $(this).attr('class', 'anotherclass'); to

$(this).addClass('anotherclass');

for add multiple classes to the field

0

精彩评论

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