gpt4 book ai didi

javascript - 有没有一种方法可以使用 HTML5 大纲算法和 CSS(可能还有 JS)生成文档导航?

转载 作者:太空狗 更新时间:2023-10-29 16:07:41 33 4
gpt4 key购买 nike

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

编辑:有没有办法在不显式编写的情况下显示 HTML 文档的链接大纲?我在想类似 \tableofcontents 的东西在 TeX 中。所以一个空的 <nav>例如,标签将填充指向页面部分的链接的无序列表。

最佳答案

对于将从文档大纲创建自动目录的 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);

关于javascript - 有没有一种方法可以使用 HTML5 大纲算法和 CSS(可能还有 JS)生成文档导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15886047/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com