我有以下JS显示所有H标签,但我只想查看Div标签中的H2标签,以便在左侧的表格中显示
var Table = function () {
setTimeout(function () {
var tocHtml = '';
var newLine, el, title, link, headingCount;
headingCount = 0;
var pageContent = $('#content :header');
if (pageContent.length === 0) {
pageContent = $('#middleColumn :header');
}
pageContent.each(function () {
el = $(this);
if (el.parent().attr("id") != "divTitle" && el.parent().attr("id") != "Table") {
title = el.text();
el.prepend('<a name="' + headingCount + '"></a>');
link = '#' + headingCount;
newLine = '<div class="table-header-' + el[0].tagName + '">' + '<a href=\'' + link + '\'>' + title + '</a></div>';
tocHtml += newLine;
headingCount += 1;
}
我只想查看主Div中的H2标签
答案 0 :(得分:0)
仅选择h2
代码 - 从更改“标题”选择器:
var pageContent = $('#content :header');
if (pageContent.length === 0) {
pageContent = $('#middleColumn :header');
}
到:
var pageContent = $('#content h2');
if (pageContent.length === 0) {
pageContent = $('#middleColumn h2');
}