如何以编程方式为我的页眉创建锚点?

m r*_*m r 8 html anchor jquery

我在页面上有几个标题,我需要在每个页面上填充一个链接到相应锚点的导航框."

但是我的标题都没有锚定.我有太多页面无法手动执行此操作.谁能想出一个干净的jquery解决方案?

Orb*_*bit 13

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>');
    });
}
Run Code Online (Sandbox Code Playgroud)