jquery - 如何以编程方式为我的页眉创建 anchor ?

标签 jquery html anchor

我在一个页面上有多个标题,我需要在每个页面上填充一个导航框以链接到相应的 anchor 。”

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

最佳答案

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>');
    });
}

关于jquery - 如何以编程方式为我的页眉创建 anchor ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5148935/

相关文章:

html - Margin-top 不适用于 <p> 和 <a> 标签?

objective-c - UIWebView 和缩放图像 - 不一致的结果?

javascript - 为 JavaScript 链接创建一个图标?

html - anchor 标签 - 链接到浏览器窗口的底部而不是顶部

jQuery 平滑滚动 anchor 导航

javascript - 如何将 = 字符更改为 "with the value"

javascript - 悬停时,如何使用 jQuery 动画更改多个属性

javascript - Instafeed 将数据过滤到不同的 div 中

html - 我的 html 没有在页面上呈现什么?

javascript - 如何使用html2canvas打印网页?