这是目录的HTML代码。因此,我尝试将其插入博客中,但问题是它正在获取帖子外的所有head标签(侧边栏小部件和作者的姓名都带有head标签)
检查https://bloginoobs.blogspot.com/2019/04/simple-table-of-contents.html了解更多详细信息...
我实际上对javascript没有任何了解,但是我尝试了一些尝试性的错误方法,但是没有任何反应。但是此代码通过获取<article>
和</article>
<script>
var ToC =
"<nav role='navigation' class='table-of-contents'>" +
"<h4>On this page:</h4>" +
"<ul>";var newLine,el,title,link;$("article h2,h3,h4").each(function(){el = $(this);title = el.text();link = "#" + el.attr("id");newLine =
"<li>" +
"<a href='" + link + "'>" +
title +
"</a>" +
"</li>";ToC += newLine}
);ToC +=
"</ul>" +
"</nav>";$(".all-questions").prepend(ToC);
</script>