Javascript-getElementsByTagName-跳过第一个元素| Sharepoint 2019-目录
我找到了一个为sharepoint网站创建内容表的脚本。它工作正常,但在我们的sharepoint网站上,第一个标题(H1)是页面标题。所以我不得不跳过第一个标题(页面标题) 有人知道我必须编辑哪个部分才能满足我的需要吗?=)Javascript-getElementsByTagName-跳过第一个元素| Sharepoint 2019-目录,javascript,sharepoint,getelementsbytagname,tableofcontents,Javascript,Sharepoint,Getelementsbytagname,Tableofcontents,我找到了一个为sharepoint网站创建内容表的脚本。它工作正常,但在我们的sharepoint网站上,第一个标题(H1)是页面标题。所以我不得不跳过第一个标题(页面标题) 有人知道我必须编辑哪个部分才能满足我的需要吗?=) #总有机碳{ 填充:10px; /*在边框和TOC内容之间添加填充*/ 背景色:#f9f9f9; /*添加TOC的背景色*/ 最小宽度:200px; /*只添加这样的最小宽度,TOC可以扩展*/ 显示:内联块; 边框:1px实心#AAAAA; /*在TOC周围添加边框
#总有机碳{
填充:10px;
/*在边框和TOC内容之间添加填充*/
背景色:#f9f9f9;
/*添加TOC的背景色*/
最小宽度:200px;
/*只添加这样的最小宽度,TOC可以扩展*/
显示:内联块;
边框:1px实心#AAAAA;
/*在TOC周围添加边框*/
列表样式:无;
/*删除默认样式列表*/
}
#李国章{
/*如果需要在所有*/
}
#sp toc li a{
文字装饰:无;
/*删除默认链接下划线*/
}
#sp toc类别-toc-h1{
/*目前,脚本支持的第一个级别*/
}
#sp toc类别-toc-h2{
/*添加比上一个缩进更大的缩进*/
左侧填充:10px;
}
#sp toc类别-toc-h3{
/*添加比上一个缩进更大的缩进*/
左侧填充:20px;
}
#sp toc类别-toc-h4{
/*添加比上一个缩进更大的缩进*/
左侧填充:30px;
}
#sp总有机碳等级-toc-h5{
/*添加比上一个缩进更大的缩进*/
左侧填充:40px;
}
#sp toc类别-toc-h6{
/*添加比上一个缩进更大的缩进*/
左侧填充:50px;
}
/**
*构建TOC的主要功能
*/
函数buildToc(){
/*初始化标题级计数器*/
var countH1=countH2=countH3=countH4=countH5=countH6=0;
/**
*doElement:由ID检索的TOC容器
*/
var toc=document.getElementById(“sp toc”);
/**
*将章节插入TOC容器中
*/
toc.innerHTML=nodesToc();
/**
*TOC生成器
*/
函数nodesToc(){
/*从当前页面获取所有HTML标记*/
var obj=document.getElementsByTagName(“*”);
var tagList=“H1;H2;H3;H4;H5;H6;”;
var str=“”;
/*在每个HTML标记上循环*/
对于(变量i=0;i=0){
/*根据当前标题获取多级列表的编号*/
var lvl=getNumberLevel(obj[i].标记名);
/*HTML模板*/
str+=“”;
obj[i].id=“title-”+i;
}
}
返回str;
}
/**
*根据标题级别获取CSS类
*/
函数getClassLvl(_标记名){
返回“class-toc-h”+_标记名.replace(/h/ig,”);
}
/**
*多级列表生成器
*/
函数getNumberLevel(_标记名){
如果(_标记名==“H1”){
countH2=countH3=countH4=countH5=countH6=0;
返回++countH1;
}else if(_标记名==“H2”){
countH3=countH4=countH5=countH6=0;
返回countH1++“+++countH2;
}else if(_标记名==“H3”){
countH4=countH5=countH6=0;/*重置下一级编号*/
返回countH1++“+countH2++”+++countH3;
}else if(_标记名==“H4”){
countH5=countH6=0;/*重置下一级编号*/
返回countH1+“+countH2+”+countH3+“+++countH4;
}else if(_标记名==“H5”){
countH6=0;/*重置下一级编号*/
返回countH1+“+countH2+”+countH3+“+countH4+”+++countH5;
}else if(_标记名==“H6”){
返回countH1+“+countH2+”+countH3+“+countH4+“+countH5+”+++countH6;
}
}
}
/*加载SP页后注册TOC函数*/
_spBodyOnLoadFunctionNames.push(“buildToc”);
来源:在以开头的for循环中
for(var i=0;i
您可以将标签放在循环的第一行(如果标签是标题标签,则放在前面)
如果(i==0)继续;
您可以判断得到的所有元素的属性值。如果元素为h1,id属性值为“pageTitle”,则跳过它
与此同时,我从代码编写者那里得到了一个提示,下面的代码完全符合我的需要
<!--
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>
#总有机碳{
填充:10px;
/*在边框和TOC内容之间添加填充*/
背景色:#f9f9f9;
/*添加TOC的背景色*/
最小宽度:200px;
/*只添加这样的最小宽度,TOC可以扩展*/
显示:内联块;
边框:1px实心#AAAAA;
/*在TOC周围添加边框*/
列表样式:无;
/*删除默认样式列表*/
}
#李国章{
/*如果需要在所有*/
}
#sp toc li a{
文字装饰:无;
/*删除默认链接下划线*/
}
#sp toc类别-toc-h1{
/*目前,脚本支持的第一个级别*/
}
#sp toc类别-toc-h2{
/*添加比上一个缩进更大的缩进*/
左侧填充:10px;
}
#sp-
<!--
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>