开发者

how to isolate one of the two classes

i have a link

<a href="" class="states state1">More Info</a>

and I want to get the class state1 and not states

I have tried this

$(".states").attr("class")

"states state1"

but when i try

>> $(".states").attr开发者_如何学C("class").not(".states")
TypeError: $(".states").attr("class").not is not a function
>>> $(".states").attr("class").not("states")
TypeError: $(".states").attr("class").not is not a function

any ideas how to get the other class


$.trim( $(".states").attr("class").replace("states", "") );

Will give you the other class.

Example: http://jsfiddle.net/L2AVd/1/


why dont u just select all that have .state1?

$('.state1').(js fn...)

or if every state (state1, 2, 3, ...) is unique, you can use ID's and get the id easily:

$('.state').each(function(){
    var stats_is = this.id;
})


A bit brute-forc-ey, but this works:

var classes = $('.states').attr('class').replace('states','').trim(' ').split(' ')
for(var i=0;i<classes.length;i++)
    alert(classes[i])

Edit: Just to re-itterate what ive already put in a comment, it sounds like you're shoe-horning everything into the class attribute, where you could easily be using another attribute and making your life alot simpler:

<a href="" class="states" data-stateId="state1">More Info</a>

now you could just go

$('.states').attr('data-stateId')


$(".states").toggleClass('states').attr("class")

jsfiddle

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜