0

我正在使用这个脚本来生成一个目录:

https://github.com/nanotube/generated_toc

我在这个测试页面上使用它。如您所见,创建的 [返回顶部] 链接非常丑陋:

http://www.utahrails.net/up/up-loco-features_toc-test.php

要将 CSS 样式应用于“[返回顶部]”链接,创建的 div 需要有一个类。

这有效:

  // create a "back to top" link
  if (back_to_top == 'on'){
    newdiv = document.createElement('div');
    newdiv.innerHTML = "<a href='#beforetoc'>[back to top]</a>";
    this_head_el.parentNode.insertBefore(newdiv, this_head_el.nextSibling);

但事实并非如此。ToC 中的内容消失了,所有的链接都消失了:

  // create a "back to top" link
  if (back_to_top == 'on'){
    newdiv = document.createElementWithClass('div', 'back-to-top');
    newdiv.innerHTML = "<a href='#beforetoc'>[back to top]</a>";
    this_head_el.parentNode.insertBefore(newdiv, this_head_el.nextSibling);

我真的是脚本新手,我看不出问题出在哪里。

4

2 回答 2

1

I don't see any reference for a createElementWithClass() method on the document object. If you were checking your javascript error console or the debug console, it should have shown you the exact error.

Just use this:

// create a "back to top" link
  if (back_to_top == 'on'){
    newdiv = document.createElement('div');
    newdiv.className = "back-to-top";
    newdiv.innerHTML = "<a href='#beforetoc'>[back to top]</a>";
    this_head_el.parentNode.insertBefore(newdiv, this_head_el.nextSibling);
于 2013-02-25T16:43:59.427 回答
0

There is no (at least no native) createElementWithClass function. Use

var newdiv = document.createElement('div');
newdiv.className = 'back-to-top';
…

(see docs for className property and var statement)

于 2013-02-25T16:44:48.433 回答