我正在使用最新的 jQuery(通过 Google CDN)并且正在使用此代码来构建 TOC:
$("#infoArticles h2, #infoArticles h3").each(function(i) {
var current = $(this);
current.attr("id", "title" + i);
$("#toc").append("<li><a id='link" + i + "' href='#title" + i + "' title='" + current.attr("tagName") + "'>" + current.html() + "</a></li>");
});
工作一种享受。
我希望 .each 循环将匹配的 h2 与匹配的 h3 区别对待,以便我可以将一个类添加到由 h2 产生的 li 中。
任何有用的意见,非常感谢和感激地接受!