解决方法
function addAnchors(){ //loop through all your headers $.each($('h1'),function(index,value){ //append the text of your header to a list item in a div,linking to an anchor we will create on the next line $('#Box-anchors').append('<li><a href="#anchor-'+index+'">'+$(this).html()+'</a></li>'); //add an a tag to the header with a sequential name $(this).html('<a name="anchor-'+index+'">'+$(this).html()+'</a>'); }); }