views:

98

answers:

4

i want that when i click this link its color changes to the given color

<li id="press"><a href="<?=base_url()?>index.php/page/press">Press</a></li>
A: 

All links? a:focus { color: orange; }

Some links? Give them a class, eg <a class="foo" ...>: a.foo:focus { color: purple; }

One link? Give it an id, eg <a id="bar" ...>: a#bar:focus { color: #BADA55; }

Anonymous
`a:focus` is incorrect. The `:focus` pseudo-class adds special style to an element that has keyboard input focus. http://www.w3schools.com/CSS/pr_pseudo_focus.asp And the question is **Change color of an anchor when clicked**
Jonny Haynes
@Haynes: `:focus` refers to caret focus, which is applied after the link becomes active (`:active` is transitory). That is, the caret moves to the last-clicked position.
Anonymous
A: 

Hi !

You can accomplish that at server-side with PHP or with JS.

With PHP all you need is to added a given classname to the link once clicked. a very simple example would be:

<a href="myURL" class="<?php if(ExpressionToDetermineIfLinkIsClicked) echo 'selected'; ?>">

and CSS:

.selected { color: #FF0000; }

If you would like to do it with JS , and you are using any JS Framework just search the frameworks' site for "How to add an event" & "How to add classname" then combine what you get to know from the search results.

If you are, by coincidence, using prototype.js framework, then you can try the following:

function selectLink(link){
  link.addClassName('selected');

  var otherLinks = link.siblings();

  for(var i = 0; i < otherLinks.lenght; i++){
     otherLinks[i].removeClassName('selected');     
  }

}

document.observe('dom:loaded', function(){
    $('menu').observe('click', function(event){
       event.stop();
       var link = Event.element(event); 
       selectLink(link);
       });
    });

---
<div id="menu">
<a href="url1" id="link1" class="">
<a href="url2" id="link2" class="">
<a href="url3" id="link3" class="">
</div>
Rossen Zahariev
+2  A: 

The CSS declaration :active will accomplish what you're after. http://www.w3schools.com/CSS/pr_pseudo_active.asp

Example.

a:active {
    color: #C00;
}

NB.

a:active MUST come after a:hover in the CSS definition in order to be effective!

Jonny Haynes
+1  A: 

Here is the sample Css for the visited hyperlink

a:link {color:#FF0000}
a:visited{color:Red}

Hope that will help.

Asim Sajjad
+1 Simplest and most widely supported cross-browser solution
slebetman
-1 It's not - Richa wants the colour to change when clicked, not after.
Jonny Haynes
Then you can usea:active{color:Red}Hope that will help
Asim Sajjad