【问题标题】:Is there a way to generate document navigation using the HTML5 outline algorithm and CSS (and possibly JS)?有没有办法使用 HTML5 大纲算法和 CSS(可能还有 JS)生成文档导航?
【发布时间】:2013-04-09 05:55:52
【问题描述】:

有没有办法使用 HTML5 大纲算法和 CSS(可能还有 JS)生成文档导航,比如 TeX 可以生成目录?

编辑:有没有一种方法可以显示 HTML 文档的链接大纲而不显式编写它?我正在考虑像 TeX 中的 \tableofcontents 之类的东西。因此,例如,一个空的<nav> 标记将填充到页面中各个部分的链接的无序列表。

【问题讨论】:

  • 也许我不明白你的问题.. 一个快速的谷歌搜索显示你在寻求帮助之前没有付出太多努力:google.ca/…
  • ... 并显示第一个结果:coding.smashingmagazine.com/2011/08/16/…
  • 我的意思是问我们如何在文档中显示目录而不显式编写它,就像 TeX 中的 /tableofcontents 一样,如果没有办法(没有 javascript)浏览器最终会提供方法?我读过那篇文章,我认为它没有解决这个问题。
  • FWIW,有一些浏览器插件在做这项工作:superuser.com/questions/400105/…

标签: javascript css html tableofcontents


【解决方案1】:

对于将从文档大纲创建自动目录的 javascript,您目前必须开发自己的目录。 [我实际上没有找到复制粘贴的解决方案]

研究这个:

还有这个

[插件]

来自用户@unor 的建议阅读:github.com/DylanFM/outliner 将我发送到此jsFiddle,那里还有另一个 javascript 启动。

Javascript

// See http://html5doctor.com/document-outlines/
// That article begins with info on HTML4 document outlines
// This doesn't do that yet, it just handles the HTML5 stuff beneath in the article
// I'm sure there are problems with handling that HTML5 stuff tho

var headingElements = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6'],
sectioningElements = ['SECTION', 'ARTICLE', 'NAV', 'ASIDE'];

function makeOutline(root) {
var ar = [],
    el = root.firstChild,
    nested, hg;
while(el) {
    // If it's a sectioning element, create a new level in the outline
    if(sectioningElements.indexOf(el.tagName) > -1) {
        nested = makeOutline(el);
        if(nested.every(function(i){ return typeof i !== 'string'; })) {
            nested.unshift('Untitled ' + el.tagName.toLowerCase());
        }
        ar.push(nested);
    } else if(headingElements.indexOf(el.tagName) > -1) {
        ar.push(el.textContent);
    } else if(el.tagName === 'HGROUP') {
        hg = undefined;
        // Find the highest heading element within
        // Use its text, otherwhise it's untitled
        try {
            headingElements.forEach(function(t) {
                els = el.getElementsByTagName(t);
                if(els.length) {
                    hg = els[0].textContent;
                    throw BreakException;
                }
            });
        } catch(e) {}
        if(!hg) {
            hg = 'Untitled hgroup';
        }
        ar.push(hg);
    }
    el = el.nextSibling;
}
return ar;
};

var outline = makeOutline(document.body);

// This is just used for displaying the outline. 
// Inspect the outline variable to see the generated array:
// console.log(outline);

function describeOutline(outline) {
var indentForDepth = function(depth) {
    var str = '';
    for(i=depth;i>0;i--) {
        str += '\t';
    }
    return str;
},
childrenAreStrings = function(ar, depth) {
    var depth = (depth && (depth + 1)) || 1;
    return ar.map(function(item) {
        if({}.toString.call(item)=='[object Array]') {
            return childrenAreStrings(item, depth).join('\n');
        } else {
            return indentForDepth(depth) + '- ' + String(item);
        }
    });
};
// Make sure all items in ar are strings
return childrenAreStrings(outline).join('\n');    
}

(document.getElementsByTagName('pre')[0]).textContent = describeOutline(outline);

【讨论】:

猜你喜欢
  • 2011-01-09
  • 2015-10-29
  • 1970-01-01
  • 2023-04-08
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-08
相关资源
最近更新 更多