如何只在JS中查看表中的H2标签

时间:2016-07-17 20:40:00

标签: javascript html

我有以下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标签

1 个答案:

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