remembering the class of an element for later



I have a series of link on my pages with random a:hover classes so they display different colors on mouse over.

When a link is clicked its set to a different class so that it looks highlighted or active.

I need to create a script that first saves the class of the clicked link and then after it is changed and a new link is clicked finds the highlighted element and changes it back to the original class it was before changing the new active link.

What is the best way to store the class of the link until the new link is called.

Something like:

Get the class of the previous highlighted link from a variable? Set the previous link to its original class.

Store the current class of the new link. Set the element as the current highlighted link.

2 个答案




当您单击特定链接时,需要将“ selected”类添加到该链接,但是首先,您需要从页面上所有链接中将“ selected”类删除为“ class”链接类”。


 $('').click(function() {
  var old_link = null;
 function store(ele){
   old_link = {
     cn: ele.className,
     r: ele
 function restore(){
   old_link.r.className =;
  • cn is the class name before editing. r is a reference to the element. I just wrote it like that so a single variable could be used to store both pieces of information.

    scragarJuly 22, 2009 20:32