Next Question

Mahalo is adding a tip to all questions that don't offer a tip.
M¢25 Funded By Mahalo ? |
June 23, 2009 04:01 PM
RSS
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
Answered Question

Mahalo is adding a tip to all questions that don't offer a tip.
Just getting ready for work... Anyone know how to simulate the :target CSS class in Javascript?
Interesting Question?
Yes (0)
No (0)
- About css |
- Via Twitter |
- In Technology & Internet |
- |
- Report |
-
Share
RSS
Best Answer Decided by Votes
| June 25, 2009 04:02 PM | view on twitter |
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
Answer this Question
Related Questions
No questions found.
Ask a Question
Buy Mahalo Dollars with Credit Card or PayPal
Top Members
Most Popular Tags
Categories
- Anonymous
- Arts & Design
- Beauty & Style
- Books & Authors
- Business
- Cars & Transportation
- Consumer Electronics
- Coupons Deals
- Education
- Entertainment
- Environment
- Fitness
- Food & Drink
- From Email
- From Iphone
- From Twitter
- Health
- History
- Hobbies
- Home & Garden
- How Tos
- Humor
- Jobs
- Legal
- Local
- Love & Relationships
- Mahalo Answers Community
- Money
- Music
- News
- NSFW
- Parenting
- Pets
- Science & Mathematics
- Services
- Shopping
- Social Science
- Society & Culture
- Sports
- Technology & Internet
- Travel
- Video Games
Welcome New Members
- jacksondarrel, November 30, 2009 09:09 AM
- vynguyen, November 30, 2009 08:59 AM
- deeb86, November 30, 2009 08:49 AM
- deebsaif, November 30, 2009 08:48 AM
- mactavish911, November 30, 2009 08:09 AM
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