Jquery To Add Ids To All Paras To Make Them All Linkable.
I have a site with thousands of htm documents on them. Sometimes I need to send someone to a specific paragraph to read. What I am looking for is a jquery plugin which simply adds
Solution 1:
I think what you want is just a simple code like this
jQuery(document).ready(function() {
jQuery('p').each(function(index, value) {
value.id = '_p' + index;
});
$('html, body').animate({
scrollTop: $(window.location.hash).offset().top
}, 500);
});
Here is an example that also displays the id set.
http://jsfiddle.net/ekftprLt/2/
Solution 2:
Why a plugin? And no need for an ID
This should work if you insert a script into the page or into an existing external script
NOTE: This does NOT modify the DOM in any way and is WAY faster than an .each
$(function() {
var pIndexPos = location.hash.indexOf("p_");
if (pIndexPos!=-1) {
var pIndex = parseInt(location.hash.substring(pIndexPos+2),10)+1; // 1based for nthvar offsetTop = $("p:nth-child("+pIndex+")").offset().top-20; // - height of item
$('html, body').animate({
scrollTop: offsetTop
},500);
}
});
Post a Comment for "Jquery To Add Ids To All Paras To Make Them All Linkable."