Ask questions via twitter! Message any question to @answers on twitter. We'll publish the question and send you a reply each time there's a new answer.
Next Question

Answered Question

 
 M¢25  Funded By Mahalo ? |  June 23, 2009 04:01 PM

Just getting ready for work... Anyone know how to simulate the :target CSS class in Javascript?

Interesting Question?  Yes (0)   No (0)   
RSS
 
 

Best Answer  Decided by Votes

 
June 25, 2009 04:02 PM | view on twitter
The :target pseudo-class in CSS lets a developer style an anchor identifier (# in a URL, also known as a fragment identifier) differently. An example of this can be seen at http://www.twisty.com/misc/tests/css/target-trick.html Note: You'll need to use a modern browser like Firefox, Chrome, or Safari in order to see how it works.

Replicating this using JavaScript is fairly simple.

var target = window.location.hash.replace('#','');
document.getElementById(target).className = 'active';

The first line grabs the fragment identifier and removes the #. Then we find the element that has the same ID as the fragment identifer and we give it a class of 'active'. Then in your CSS you can style the selector .active any way you want.
Source(s):
http://www.w3.org/TR/css3-selectors/#target-pseudo
http://www.twisty.com/misc/tests/css/target-trick.html#b



Tags: fragment, identifier, javascript, anchor, css

Helpful Answer?  (0)   (0)    Tip kingkool68 for this answer
Permalink | Report
Voted as best: jfesmire, matthewh, bestpay
   Reply  
 
 

Answer this Question

How tips and payments work

This question has already been resolved. You may add an answer to it but you will not be eligible to win best answer or any associated tips.

Related Questions

No questions found.

Ask a Question


140 characters left
Top of Page
Buy Mahalo Dollars with Credit Card or PayPal

Top Members

This Week All Time
  • buddawiggi
    buddawiggi
    2nd Degree Black Belt
    27808 Points
    M$799.16 Earned
  • opher
    opher
    Purple Belt
    4473 Points
    M$196.22 Earned
  • annelisle
    annelisle
    Purple Belt
    3110 Points
    M$95.22 Earned
   See All
 

Most Popular Tags

mahalo(1631)
iphone(466)
music(463)
google(358)
food(321)
online(298)
beer(279)
money(263)
movies(262)
apple(252)
aotd(235)
health(220)
video(208)
free(206)
dog(205)
   See All
 

Categories

Welcome New Members


 
 
Mahalo Dollars are the currency of Mahalo Answers.

Each Mahalo Dollar costs $1.

Once you earn more than 40 Mahalo Dollars, you can request to be paid via PayPal. Each Mahalo Dollar is currently worth $0.75 when paid out via PayPal. Learn More

 
 

Please log in to use this function.