I copied the superscript Wikipedia verbatim:
<sup class="reference" id="cite_ref-1"> <a href="#cite_note-1"> <span>[</span>1<span>]</span> </a> </sup>
And used the following jQuery:
$(".reference > a").click(function() { var href = $(this).attr("href"); var $el = $(href); $(".ref-highlight").removeClass("ref-highlight"); $el.addClass("ref-highlight"); });
The key href captures the turned off link that was clicked, and then using it to select an element and apply the selection class.
In addition, you do not need to scroll down the page programmatically, because the browser automatically processes this for you.
Here is a complete working example: http://jsfiddle.net/andrewwhitaker/dkWJm/2/
Update: I noticed that in your comment below you want to animate scrolling, here is how you do it:
$(".reference > a").click(function(event) { event.preventDefault(); var href = $(this).attr("href"); var $el = $(href); $(".ref-highlight").removeClass("ref-highlight"); $el.addClass("ref-highlight"); $("html, body").animate({"scrollTop": $el.offset().top}, 3000); });
Notes:
- We cancel the default action in this case (using
event.preventDefault() ), which would have to go to the element using the id corresponding to the clicked href link. - Using
animate , smoothly scroll the html element to the appropriate position, which is determined using offset() on the target element. - The
duration argument (I specified 3000) is what you would like to adjust to lengthen / shorten the scroll duration.
Here is an example: http://jsfiddle.net/andrewwhitaker/dkWJm/4/ (Updated to work in IE8 / Chrome / FF 3)
source share