remembering the class of an element for later

Asked
Viewd56

0

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 个答案

0

我会略有不同。

对于要包含的任何链接,它们都应具有相同的类,例如“链接类”。

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

在jquery中,它看起来像

 $('a.link-class').click(function() {
    $('a.link-class').removeClass('selected');
    $(this).addClass('selected');
});
 
0
  var old_link = null;
 function store(ele){
   old_link = {
     cn: ele.className,
     r: ele
   }
 }
 function restore(){
   old_link.r.className = old_link.cn;
 }
 
  • 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