createElement vs. createElementWithClass

createElement vs. createElementWithClass

本文关键字:createElementWithClass vs createElement      更新时间:2023-09-26

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

https://github.com/nanotube/generated_toc

我在这个测试页面上使用它。正如你所看到的,创建的[back to top]链接真的很难看:

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

要应用CSS样式到"[back To top]"链接,创建的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);

我对编写脚本很陌生,我看不出问题在哪里。

我在文档对象上没有看到任何createElementWithClass()方法的引用。如果您正在检查javascript错误控制台或调试控制台,它应该会显示准确的错误。

就用这个:

// 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);

没有(至少没有本地)createElementWithClass函数。使用

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

(参见className属性和var声明文档)

相关文章:
  • 没有找到相关文章