开发者

Jquery Relace Part of String in Any Class

I am trying to reset a class for any links that have been previously selected by removing the end of a class name.

When the link is clicked on the class is appended with开发者_C百科 "_selected" so that the image remains highlighted. When another link is clicked on, I am trying to remove "_selected" from any link that may have been selected previously (many rollovers on the page).

Seems like the following should work:

$('a').attr('class').replace(/_selected/g, '');

Doesn't this look for all links with classes and replace instances of "_selected" with nothing?

Is there a better way to look at this? New to jQuery... Thanks


You can put more than one classes in an element, like:

<a class="someClass selected" />

So, with jQuery:

$("a.selected").removeClass("selected");

and of course previously you have added it with

$("your-selector").addClass("selected");

And you need to define it, of course

<style>
 A.selected {
     /*your styles here*/
 }
</style>

Not exactly what you want, but I think it is what you need :)

In fact, what you want is:

$("a").each(function(){
    $(this).attr("class", $(this).attr("class").replace(/_selected/g,""));
});


$('a').attr('class') will return the 'class' string for each jQuery 'a' elements and not the element itself...

I think you should do something like that :

$('a').each(function(){

$(this).attr('class', $(this).attr('class').replace(/_selected/g, ''));

});

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜