Beispiel:Hybride Nummerierung.html

Aus SELFHTML-Wiki
Wechseln zu: Navigation, Suche
<!DOCTYPE html>
<html lang="de">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="stylesheet" media="screen" href="./Beispiel:SELFHTML-Beispiel-Grundlayout.css">
	<title>dynamisch erstelltes Inhaltsverzeichnis</title>
	<style>
[aria-labelledby="sections-heading"] {
	background: #F1F3F4;
	border: thin solid #d5d5d5;
	border-radius: 0 0.7em 0.7em 0.7em;
	margin-left: 1em;
	float: right;
	width: 15em;
	padding: .2em 1em .3em 1em;
}

[aria-labelledby="sections-heading"] .toc-level-2 {
	margin-left: 1em;
}

[aria-labelledby="sections-heading"] .toc-level-3 {
	margin-left: 2em;
}

[aria-labelledby="sections-heading"] .toc-level-4 {
	margin-left: 3em;
}

[aria-labelledby="sections-heading"] .toc-level-5 {
	margin-left: 4em;
}

body {
	counter-reset: section;
	margin: 2em auto;
}
h2 {
	counter-reset: subsection;
}
h2:before {
	counter-increment: section;
	content: "Band " counter(section) " - ";
	color:red;
}
h3:before {
	counter-increment: subsection;
	content: counter(section) "." counter(subsection) ": ";
	color:green;
}

h6 {
	font-size: 1.5rem;
	margin: 0.5rem;
	}
	</style>
<script>

document.addEventListener('DOMContentLoaded', function () {

function generateToC() {
	
		document.querySelector('body').insertAdjacentHTML('afterbegin', `
		<nav aria-labelledby="sections-heading" aria-label="Table of Contents">
			<h6 id="sections-heading">Inhaltsverzeichnis</h6>
			<ul>
			</ul>
		</nav>
	`);

	// alle Überschriften sammeln
  document.querySelectorAll('h2, h3, h4, h5').forEach(heading => {
		const
			ul = document.querySelector('[aria-labelledby="sections-heading"] ul'),
			li = ul.appendChild(document.createElement('li')),
			a = li.appendChild(document.createElement('a'));

		// erzeuge eine ID falls keine vorhanden
		if (!heading.hasAttribute('id')) {
			// verwende Textinhalt der Überschrift in Kleinbuchstaben
			heading.id = heading.textContent.toLowerCase();
		}

		a.href = '#' + heading.id;
		a.appendChild(document.createTextNode(heading.textContent));

		// Ebene für Einrückungen
		if (heading.tagName != 'H2') {
			li.classList.add(
				'toc-level-' + (
					parseInt(heading.tagName.replace(/h/i, ''))
					-1
				).toString()
			);
		}
	});
}

generateToC();

});

</script>
</head>
<body>
<h1>dynamisch erstelltes Inhaltsverzeichnis</h1>

	<h2>HTML</h2>
	<h3>Hyperlinks</h3>
	<h3>Elemente</h3>
	<h3 id="schon vorhanden">Tutorials</h3>
	<h2>CSS</h2>
	<h3>Eigenschaften</h3>
	<h3>Selektoren</h3>
	<h3>Tutorials</h3>
	<h2>JavaScript</h2>
	<h3>Funktionen</h3>
	<h3>Objekte</h3>
	<h3>APIs</h3>
	<h3 id="tutorials3">Tutorials</h3>

<p>Zum Tutorial: <a href="https://wiki.selfhtml.org/wiki/HTML/Tutorials/Listen/Hybride_Nummerierung">Listen - Hybride Nummerierung</a>.</p>

<a href="https://forum.selfhtml.org/advent/2024"><div style="display:grid;grid-template-columns: 63px 1fr; gap:0.5em;"><img src="https://wiki.selfhtml.org/images/a/ad/Selfhtml-nico.png" alt="SELFHTML-Logo mit Adventsmütze" width="63px"><p>Zurück zum <br><b>Adventskalender 2024</b>.</p></div></a>
</body>
</html>