我在一个页面上有多个标题,我需要在每个页面上填充一个导航框以链接到相应的 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/