Js和CSS实现脚注(Footnote)效果

2016-02-19 19:47 12 1 收藏

下面这个Js和CSS实现脚注(Footnote)效果教程由图老师小编精心推荐选出,过程简单易学超容易上手,喜欢就要赶紧get起来哦!

【 tulaoshi.com - Web开发 】

  脚注(Footnote)是向用户提供更多信息的一个最佳途径,也是主体信息的一个有效补充,常见于各种印刷书籍中。不过,既然脚注有这些好处,我们当然要在网页中也加以利用,本文向您介绍了用Javascript和CSS实现脚注效果的方法。

  Javascript:

script type="text/javascript"
// 说明:用 Javascript 和 CSS 实现脚注(Footnote)效果
// 作者:CodeBit.cn ( http://www.CodeBit.cn )
var footNotes = function(){};
footNotes.prototype = {
  footNoteClassName : "footnote",  // 脚注的 className
  footNoteTagName : "span",  // 脚注的标签名
  footNoteBackLink : " [back]",  // 返回链接
  format : function(contentID, footnoteID)
  {
    if (!document.getElementById) return false;
    var content = document.getElementById(contentID);
    var footnote = document.getElementById(footnoteID);
    var spans = content.getElementsByTagName(this.footNoteTagName);
    var noteArr = [];
    var note = 0;
    var elContent;
    var len = spans.length;
    for (i=0; ilen; i++)
    {
      note ++;
      if (spans[i].className == this.footNoteClassName)
      {
        // 获取脚注内容
        elContent = spans[i].innerHTML;
        noteArr.push(elContent);
        // 创建一个指向脚注的链接
        var newEle = document.createElement( "a" );
        newEle.href = '#ftn_' + footnoteID + '_' + note;
        newEle.title = "show footnote";
        newEle.id = 'ftnlink_'+footnoteID+'_' + note;
        newEle.innerHTML = note;
        // 清空原有内容
        while (spans[i].childNodes.length)
        {
          spans[i].removeChild( spans[i].firstChild );
        }
        spans[i].appendChild( newEle );
      }
    }
    // 创建注释列表
    var ul = this.__buildNoteList(noteArr, footnoteID);
    footnote.appendChild(ul);
  },
  __buildNoteList : function(notes, noteID) 
  {
    if(!notes || notes.length 1) return;
    var ul = document.createElement('ul');
    ul.className = this.footNoteClassName;
    var li;
    var len = notes.length + 1;
    for(i=1; ilen; i++)
    {
      li = document.createElement('li');
      li.id = "ftn_"+noteID+"_"+i;
      li.innerHTML = notes[i-1];
      // 创建链接
      var link = document.createElement("a");
      link.href = "#ftnlink_" + noteID + "_" + i;
      link.innerHTML = this.footNoteBackLink;
      li.appendChild( link );
      ul.appendChild( li );
    }
    return ul;
  }
};
/script

  要实现脚注,我们需要下列元素:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

  HTML:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

div id="article1"
CSS span class="footnote"CSS 是 Cascading Style Sheet 的缩写。译作「层叠样式表单」。是用于(增强)控制网页样式并允许将样式信息与网页内容分离的一种标记性语言。/span
/div
div id="artnotes1" class="footnoteHolder"/div

  其中:

  article1是你需要脚注的文章主体

  spanclass="footnote"../span是注释内容,标签span和class均可配置。

  artnotes1是显示脚注的地方

  按照默认的设置调用方式:

  Javascript:

script type="text/javascript"
  var footnote = new footNotes();
  footnote.format('article1','artnotes1');
/script

  如果你想自定义一些内容,可以用下面的方式:

  Javascript:

  script type="text/javascript"
  var footnote = new footNotes();
  footnote.footNoteClassName = "footnote2";
  footnote.footNoteTagName = "em";
  footnote.footNoteBackLink = " [back «]";
  footnote.format('article1','artnotes1');
  /script

来源:https://www.tulaoshi.com/n/20160219/1621798.html

延伸阅读
标签: Web开发
以下是引用片段: script type="text/javascript" //Url: http://bbs.51js.com/thread-61646-1-1.html //Author: 风云突变 //Modify: 枫岩 var act; function over(s,nMax){   var obj=document.getElementById(s);   var h = parseInt(obj.offsetHeight);   if (h nMax){     obj.style.height = (h ...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN"   <head    <meta name="ve...
标签: Web开发
有个静态页面 span id=ababcde/spanspan id=ababcde/spanspan id=ababcde/spanspan id=ababcde/span 能否通过js实现,IE浏览时的代码被替换为 img src="img/1.gif"/img src="img/1.gif"/img src="img/1.gif"/img src="img/1.gif"/ 无标题文档 移到我这儿来 [Ctrl+A 全选 注:...
标签: Web开发
缓动,学名为Tween,缓冲移动的简称。要想页面内容切换起来舒服,就使用淡入淡出特效,要想让页面元素动起来自然,就要使用缓动效果。这两个混合起来,可以衍生多种特效的。感谢Flash开发人员为我们做了那么多先行研究,我们直接把它们拆出来装在各种菜单与相册中。我们先从最简单的东西做起,加速与减速。 既然是缓动,它就一定涉及以下概...
标签: Web开发
SCRIPT language=JAVASCRIPTfunction fade_in(){if (test.filters.alpha.opacity 100){test.filters.alpha.opacity += 10;clearTimeout(timer);var timer = setTimeout("fade_in()",150);timer;}}/SCRIPT

经验教程

14

收藏

98
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部