【问题标题】:Javascript - getElementsByTagName - skip first element | Sharepoint 2019 - Table of contentJavascript - getElementsByTagName - 跳过第一个元素 | Sharepoint 2019 - 目录
【发布时间】:2021-04-11 09:28:57
【问题描述】:

我找到了一个为共享点站点创建目录的脚本。它工作正常,但在我们的共享点站点上,第一个标题 (H1) 是页面标题。所以我必须跳过第一个标题(页面标题)。

有人知道我必须编辑哪个部分来满足我的需要吗? =)

    <!-- 
The TOC Container.
The most important thing to note is the ID.
The ID is used by the CSS and the JavaScript code.
-->
<ul id="sp-toc"></ul>

<style type="text/css">
    #sp-toc {
        padding: 10px;
        /* Add a padding between the border and the TOC content */
        background-color: #f9f9f9;
        /* Add a background color of the TOC */
        min-width: 200px;
        /* Add only a minimal width like that, the TOC can be extensible */
        display: inline-block;
        border: 1px solid #aaaaaa;
        /* Add a border arround the TOC */
        list-style: none;
        /* Remove default style list */
    }

    #sp-toc li {
        /* If you need to add custom style on all <li> */
    }

    #sp-toc li a {
        text-decoration: none;
        /* Remove default link underline */
    }

    #sp-toc .class-toc-h1 {
        /* currently, the first level supported by the script */
    }

    #sp-toc .class-toc-h2 {
        /* Add a indentation greatter than the previous one */
        padding-left: 10px;
    }

    #sp-toc .class-toc-h3 {
        /* Add a indentation greatter than the previous one */
        padding-left: 20px;
    }

    #sp-toc .class-toc-h4 {
        /* Add a indentation greatter than the previous one */
        padding-left: 30px;
    }

    #sp-toc .class-toc-h5 {
        /* Add a indentation greatter than the previous one */
        padding-left: 40px;
    }

    #sp-toc .class-toc-h6 {
        /* Add a indentation greatter than the previous one */
        padding-left: 50px;
    }
</style>
<script type="text/javascript">
    /**
     * The main function to build TOC
     */
    function buildToc() {
        /* Init title level counters */
        var countH1 = countH2 = countH3 = countH4 = countH5 = countH6 = 0;

        /**
         * DOMElement : the TOC container retrieved by ID
         */
        var toc = document.getElementById("sp-toc");

        /**
         * Insert into TOC container the chapters
         */
        toc.innerHTML = nodesToc();

        /**
         * TOC Builder
         */
        function nodesToc() {
            /* Get all HTML tags from the current page */
            var obj = document.getElementsByTagName("*");
            var tagList = "H1;H2;H3;H4;H5;H6;";
            var str = "";
            /* Loop on each HTML tag */
            for (var i = 0; i < obj.length; i++) {
                /* If tag is a title tag */
                if (tagList.indexOf(obj[i].tagName + ";") >= 0) {
                    /* Get the number of the multilevel list in accordance with the current title */
                    var lvl = getNumberLevel(obj[i].tagName);
                    /* HTML template */
                    str += "<li class='" + getClassLvl(obj[i].tagName) + "'><a href='#title-" + i + "'>" + lvl + " " + obj[i].innerHTML + "</a></li>";
                    obj[i].id = "title-" + i;
                }
            }
            return str;
        }

        /**
         * Get CSS class in accordance with the title level
         */
        function getClassLvl(_tagName) {
            return "class-toc-h" + _tagName.replace(/h/ig, '');
        }

        /**
         * Multilevel list generator
         */
        function getNumberLevel(_tagName) {
            if (_tagName === "H1") {
                countH2 = countH3 = countH4 = countH5 = countH6 = 0;
                return ++countH1;
            } else if (_tagName === "H2") {
                countH3 = countH4 = countH5 = countH6 = 0;
                return countH1 + "." + ++countH2;
            } else if (_tagName === "H3") {
                countH4 = countH5 = countH6 = 0; /* Reset next level number */
                return countH1 + "." + countH2 + "." + ++countH3;
            } else if (_tagName === "H4") {
                countH5 = countH6 = 0; /* Reset next level number */
                return countH1 + "." + countH2 + "." + countH3 + "." + ++countH4;
            } else if (_tagName === "H5") {
                countH6 = 0; /* Reset next level number */
                return countH1 + "." + countH2 + "." + countH3 + "." + countH4 + "." + ++countH5;
            } else if (_tagName === "H6") {
                return countH1 + "." + countH2 + "." + countH3 + "." + countH4 + "." + countH5 + "." + ++countH6;
            }
        }
    }
    /* Register TOC function after SP page is loaded */
    _spBodyOnLoadFunctionNames.push("buildToc");
</script>

来源:https://blog.lsonline.fr/2018/12/23/create-a-dynamic-table-of-content-for-your-sharepoint-wiki-pages/

【问题讨论】:

  • 从 1 而不是 0 开始循环?您尝试了哪些方法,哪些方法无效?
  • “我没有头绪,请帮我做。”不是SO的范围
  • 不要迭代实时 HTMLCollection,使用静态 NodeList(由document.querySelectorAll 返回)或数组。

标签: javascript sharepoint getelementsbytagname tableofcontents


【解决方案1】:

在以

开头的 for 循环内部

for (var i = 0; i &lt; obj.length; i++) {

你可以放在循环的第一行(如果标签是标题标签之前)

if (i === 0) continue;

【讨论】:

  • 感谢您的回答!我试过你的版本,但它导致一个空的 ToC-Div,sry。类似于我的回答@atmos_msft,我目前没有足够的时间来寻找更多细节。我以后会这样做的。
【解决方案2】:

你可以判断你得到的所有元素的属性值。如果元素为h1且id属性值为“pageTitle”,则跳过。

【讨论】:

  • 感谢您的回答,但目前我没有足够的时间来学习如何使用它,但我肯定会这样做。
【解决方案3】:

与此同时,我得到了代码编写者的提示,下面的代码完全符合我的需求。

<!-- 
The TOC Container.
The most important thing to note is the ID.
The ID is used by the CSS and the JavaScript code.
-->
<ul id="sp-toc"></ul>

<style type="text/css">
    #sp-toc {
        padding: 10px;
        /* Add a padding between the border and the TOC content */
        background-color: #f9f9f9;
        /* Add a background color of the TOC */
        min-width: 200px;
        /* Add only a minimal width like that, the TOC can be extensible */
        display: inline-block;
        border: 1px solid #aaaaaa;
        /* Add a border arround the TOC */
        list-style: none;
        /* Remove default style list */
    }

    #sp-toc li {
        /* If you need to add custom style on all <li> */
    }

    #sp-toc li a {
        text-decoration: none;
        /* Remove default link underline */
    }

    #sp-toc .class-toc-h1 {
        /* currently, the first level supported by the script */
    }

    #sp-toc .class-toc-h2 {
        /* Add a indentation greatter than the previous one */
        padding-left: 10px;
    }

    #sp-toc .class-toc-h3 {
        /* Add a indentation greatter than the previous one */
        padding-left: 20px;
    }

    #sp-toc .class-toc-h4 {
        /* Add a indentation greatter than the previous one */
        padding-left: 30px;
    }

    #sp-toc .class-toc-h5 {
        /* Add a indentation greatter than the previous one */
        padding-left: 40px;
    }

    #sp-toc .class-toc-h6 {
        /* Add a indentation greatter than the previous one */
        padding-left: 50px;
    }
</style>
<script type="text/javascript">
    /**
     * The main function to build TOC
     */
    function buildToc() {
        /* Init title level counters */
        var countH1 = countH2 = countH3 = countH4 = countH5 = countH6 = 0;

        /**
         * DOMElement : the TOC container retrieved by ID
         */
        var toc = document.getElementById("sp-toc");

        /**
         * Insert into TOC container the chapters
         */
        toc.innerHTML = nodesToc();

        /**
         * TOC Builder
         */
        function nodesToc() {
            /* Get all HTML tags from the current page */
            var obj = document.getElementsByTagName("*");
            var tagList = "H1;H2;H3;H4;H5;H6;";
            var str = "";
            var pageTitle = false;
            /* Loop on each HTML tag */
            for (var i = 0; i < obj.length; i++) {
                /* If tag is a title tag */
                if (tagList.indexOf(obj[i].tagName + ";") >= 0) {
                    if ('H1' == obj[i].tagName && !pageTitle) {
                        pageTitle = true;
                    } else {
                        /* Get the number of the multilevel list in accordance with the current title */
                        var lvl = getNumberLevel(obj[i].tagName);
                        /* HTML template */
                        str += "<li class='" + getClassLvl(obj[i].tagName) + "'><a href='#title-" + i + "'>" + lvl + " " + obj[i].innerHTML + "</a></li>";
                        obj[i].id = "title-" + i;
                    }
                }
            }
            return str;
        }

        /**
         * Get CSS class in accordance with the title level
         */
        function getClassLvl(_tagName) {
            return "class-toc-h" + _tagName.replace(/h/ig, '');
        }

        /**
         * Multilevel list generator
         */
        function getNumberLevel(_tagName) {
            if (_tagName === "H1") {
                countH2 = countH3 = countH4 = countH5 = countH6 = 0;
                return ++countH1;
            } else if (_tagName === "H2") {
                countH3 = countH4 = countH5 = countH6 = 0;
                return countH1 + "." + ++countH2;
            } else if (_tagName === "H3") {
                countH4 = countH5 = countH6 = 0; /* Reset next level number */
                return countH1 + "." + countH2 + "." + ++countH3;
            } else if (_tagName === "H4") {
                countH5 = countH6 = 0; /* Reset next level number */
                return countH1 + "." + countH2 + "." + countH3 + "." + ++countH4;
            } else if (_tagName === "H5") {
                countH6 = 0; /* Reset next level number */
                return countH1 + "." + countH2 + "." + countH3 + "." + countH4 + "." + ++countH5;
            } else if (_tagName === "H6") {
                return countH1 + "." + countH2 + "." + countH3 + "." + countH4 + "." + countH5 + "." + ++countH6;
            }
        }
    }
    /* Register TOC function after SP page is loaded */
    _spBodyOnLoadFunctionNames.push("buildToc");
</script>

来源:https://gitlab.lsonline.fr/SharePoint/javascript-customactions/-/snippets/83

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 2014-01-08
    相关资源
    最近更新 更多