Javascript为编号的目录

时间:2017-08-30 09:56:58

标签: javascript css sharepoint tableofcontents

我正在整理一个Sharepoint wiki,在每篇文章中我都有一个维基百科,比如标题中的目录,使用我认为是在InterWebz上找到的javascript。

<style>
#toc {
 display: table;
 border: 1px solid #aaa;
 background-color: #f9f9f9;
 font-size: 95%;
 padding: 7px;
}
#toc #tocHeader  {
 font-weight: bold;
 text-align: center;
}
#toc a:before { /* content:"• "; */ }
#toc a { line-height: 15px; margin: 10px; }
#toc .toc_Level1 { margin-left: 5px; }
#toc .toc_Level2 { margin-left: 15px; }
#toc .toc_Level3 { margin-left: 25px; }
#toc .toc_Level4 { margin-left: 35px; }
</style> 
<div id="toc"></div> 
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script type="text/javascript"> 
function GenerateTOC() {
  $("#toc").append('<p id="tocHeader">Contents</p>');
  $(".ms-rteElement-H1, .ms-rteElement-H1B, .ms-rteElement-H2, .ms-rteElement-H2B, .ms-rteElement-H3, .ms-rteElement-H3B, .ms-rteElement-H4, .ms-rteElement-H4B").each(function(i) {
 var currentNode = $(this);
  currentNode.attr("id", "title" + i);
 var linkClass = (currentNode.hasClass('ms-rteElement-H1') || currentNode.hasClass('ms-rteElement-H1B'))
   ? "toc_Level1" 
   : (currentNode.hasClass('ms-rteElement-H2') || currentNode.hasClass('ms-rteElement-H2B'))
   ? "toc_Level2" 
   : (currentNode.hasClass('ms-rteElement-H3') || currentNode.hasClass('ms-rteElement-H3B'))
   ? "toc_Level3" 
   : (currentNode.hasClass('ms-rteElement-H4') || currentNode.hasClass('ms-rteElement-H4B'))
   ? "toc_Level4" 
   : "";
  $("#toc").append("<a id='link'" + i + "' class='" + linkClass + "' href='#title" + i + "' title='" + currentNode.attr("tagName") + "'>" + currentNode.html() + "</a><br>");
  currentNode.append(" <a href='#tocHeader'>[top]</a>");
  });
}
_spBodyOnLoadFunctionNames.push('GenerateTOC');
</script>

我的目的是为每个缩进级别单独编号的目录,例如:

1. Heading 1   
  1.1. Heading 2    
  1.2. Heading 2   
2. Heading 1
  2.1. Heading 2   
  2.2. Heading 2

虽然我或多或少了解这段代码中发生了什么,但我完全不了解javascript(或者就此问题进行网页开发 - 我不知道我现在做了什么,说实话),所以我很少知道如何实现它。我可以看到该函数已经有一个计数器来生成内部链接。

我的策略是添加两个额外的变量,比如说J和K(我只有两个缩进级别),其中J只有当它是1级标题时才会递增(Element-H1,toc_Level1)并且K会增加总是但是只要处理了1级标题(Element-H1,toc_Level1)就会重置为0,但是我完全不知道如何实现这一点。

修改

我添加了javascript的其余部分,但是他们只定义了目录的格式,所以我一开始并不认为它们是相关的。

该网站的结构如下:

<span class="ms-rteElement-Name">NAME</span>
<hr/>
<span class="ms-rteElement-Section">Section1</span>
<hr/>
<span class="ms-rteStyle-Text">Stuff</span>
<p><span class="ms-rteElement-Section">Section2</span></p>
<hr/>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<p><span class="ms-rteElement-H1">Heading 1</span></p>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<p><span class="ms-rteElement-H2">Heading 2</span></p>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<p><span class="ms-rteElement-H2">Heading 2</span></p>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<p><span class="ms-rteElement-H1">Heading 1</span></p>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<p><span class="ms-rteElement-H2">Heading 2</span></p>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<p><span class="ms-rteElement-H2">Heading 2</span></p>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<span class="ms-rteElement-Section">Section3</span>
<hr/>
<p><span class="ms-rteStyle-Text">More fluff</span></p>

上面给出的脚本完美地生成了目录,当然没有前面的数字。

3 个答案:

答案 0 :(得分:0)

可能的解决方案

function GenerateTOC() {
  $("#toc").append('<p id="tocHeader">Contents</p>');
  $(".ms-rteElement-H1, .ms-rteElement-H1B, .ms-rteElement-H2, .ms-rteElement-H2B, .ms-rteElement-H3, .ms-rteElement-H3B, .ms-rteElement-H4, .ms-rteElement-H4B").each(function(i) {
    var currentNode = $(this);
    currentNode.attr("id", "title" + i);
    var linkClass = (currentNode.hasClass('ms-rteElement-H1') || currentNode.hasClass('ms-rteElement-H1B')) ?
      "toc_Level1" :
      (currentNode.hasClass('ms-rteElement-H2') || currentNode.hasClass('ms-rteElement-H2B')) ?
      "toc_Level2" :
      (currentNode.hasClass('ms-rteElement-H3') || currentNode.hasClass('ms-rteElement-H3B')) ?
      "toc_Level3" :
      (currentNode.hasClass('ms-rteElement-H4') || currentNode.hasClass('ms-rteElement-H4B')) ?
      "toc_Level4" :
      "";
    $("#toc").append("<a id='link'" + i + "' class='" + linkClass + "' href='#title" + i + "' title='" + currentNode.attr("tagName") + "'>" + currentNode.html() + "</a><br>");
    currentNode.append(" <a href='#tocHeader'>[top]</a>");
  });

  // level 1
  $(".ms-rteElement-H1, .ms-rteElement-H1B").each(function(index, el) {
    $(this).html(index + '. ' + $(this).text());
    // level 2
    $('.ms-rteElement-H2, .ms-rteElement-H2B', this).each(function(idx, child) {
      $(this).html(index + '.' + idx + ' ' + $(this).text());
    });
  });
}

答案 1 :(得分:0)

生成有序列表的解决方案,该列表应该能够处理更深层次的文档:

&#13;
&#13;
function levelClassName(level) {
  if (typeof level !== "number") {
    throw new Error("Wrong argument type");
  }

  return "ms-rteElement-H".concat(level.toString(10));
}

function elementLink(element) {
  return $("<li></li>").text($(element).text());
}

function generateTOC() {
  var level = 1, // list level
    tocList = $("<ol></ol>"), // top level list
    lastElement = tocList;

  $("#toc").append('<p id="tocHeader">Contents</p>');
  $("#toc").append(tocList);

  $("span").each(function(index, value) {
    // make a jQuery object out of current element once instead of doing it
    // every time I want to use it
    var element = $(value),
      newElement;

    if (element.hasClass(levelClassName(level))) { // same level
      newElement = elementLink(value);
      // append inside list tag or next to last tag if it isn't a list
      lastElement[lastElement.prop("tagName") === "OL" ?
        "append" :
        "after"](newElement);
      lastElement = newElement;
    } else if (element.hasClass(levelClassName(level + 1))) { // deeper
      newElement = elementLink(value);
      // make a new list tag for the deeper level
      lastElement.append($("<ol></ol>").append(newElement));
      lastElement = newElement;
      level += 1;
    } else if (level - 1 >= 1 && // shallower
      element.hasClass(levelClassName(level - 1))) {
      newElement = elementLink(value);
      lastElement // list element
        .parent().parent() // -> list (on same level) -> list element above
        .after(newElement);
      lastElement = newElement;
      level -= 1;
    }
  });
}

$(document).ready(generateTOC);
&#13;
#toc {
  display: table;
  border: 1px solid #aaa;
  background-color: #f9f9f9;
  font-size: 95%;
  padding: 7px;
}

#toc #tocHeader {
  font-weight: bold;
  text-align: center;
}

#toc a:before {
  /* content:"• "; */
}

#toc a {
  line-height: 15px;
  margin: 10px;
}

#toc .toc_Level1 {
  margin-left: 5px;
}

#toc .toc_Level2 {
  margin-left: 15px;
}

#toc .toc_Level3 {
  margin-left: 25px;
}

#toc .toc_Level4 {
  margin-left: 35px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="toc"></div>
<span class="ms-rteElement-Name">NAME</span>
<hr/>
<span class="ms-rteElement-Section">Section1</span>
<hr/>
<span class="ms-rteStyle-Text">Stuff</span>
<p><span class="ms-rteElement-Section">Section2</span></p>
<hr/>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<p><span class="ms-rteElement-H1">Heading 1</span></p>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<p><span class="ms-rteElement-H2">Heading 2</span></p>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<p><span class="ms-rteElement-H2">Heading 2</span></p>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<p><span class="ms-rteElement-H1">Heading 1</span></p>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<p><span class="ms-rteElement-H2">Heading 2</span></p>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<p><span class="ms-rteElement-H2">Heading 2</span></p>
<p><span class="ms-rteStyle-Text">Fluff</span></p>
<span class="ms-rteElement-Section">Section3</span>
<hr/>
<p><span class="ms-rteStyle-Text">More fluff</span></p>
&#13;
&#13;
&#13;

没有代码可以使链接可点击。最好的方法是为每个标题提供唯一的ID,然后在ToC中链接到它们。

答案 2 :(得分:0)

所以我终于明白了。不幸的是,没有一个答案给出了预期的结果(一个产生了ToC,其中第二级子弹也错过了相应的更高级子弹,超链接功能丢失;另一个完全没有编号)。

解决方案是在GenerateTOC()函数

下添加其他变量
 var j = 0, k = 0;

然后根据linkClass变量的值放入条件计数器和计数器重置并将它们放在单个文本变量中

 linkClass == "toc_Level1"     
   ? (j++, k = 0)       
   : linkClass == "toc_Level2"       
   ? k++       
   : "";

 var l = k == 0 
   ? (String(j) + ". ")   
   : (String(j) + "." + String(k) + ". ");

然后将文本变量添加到&#34; currentNode.html&#34;前面。在创建ToC条目的行中。

<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>    
<script type="text/javascript">    
function GenerateTOC() {    
 var j = 0, k = 0; // counters    

  $("#toc").append('<p id="tocHeader">Contents</p>');      
  $(".ms-rteElement-KD_Heading1, .ms-rteElement-KD_Heading2").each(function(i) {

 var currentNode = $(this);

  currentNode.attr("id", "title" + i);

 var linkClass = (currentNode.hasClass('ms-rteElement-KD_Heading1') || currentNode.hasClass('ms-rteElement-H1B'))    
   ? "toc_Level1"    
   : (currentNode.hasClass('ms-rteElement-KD_Heading2') || currentNode.hasClass('ms-rteElement-H2B'))    
   ? "toc_Level2"    
   : "";

 linkClass == "toc_Level1"     // runs the counters
   ? (j++, k = 0)       
   : linkClass == "toc_Level2"       
   ? k++       
   : "";

 var l = k == 0     // concatenates the counters
   ? (String(j) + ". ")       
   : (String(j) + "." + String(k) + ". ");

  $("#toc").append("<a id='link'" + i + "' class='" + linkClass + "' href='#title" + i + "' title='" + currentNode.attr("tagName") + "'>" + l + currentNode.html() + "</a><br>");    
  currentNode.append(" <a href='#tocHeader'>[top]</a>");    
  });    
}    
_spBodyOnLoadFunctionNames.push('GenerateTOC');    
</script>