forked from w3c/miniapp-implementations-meetup-2023
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
32 lines (30 loc) · 1013 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(function () {
var createEl = function (tagName, attrs) {
var anchor = document.createElement(tagName);
Object.keys(attrs).forEach(function (key) {
anchor[key] = attrs[key];
});
return anchor;
};
var createAnchorFromHeading = function (headingEl) {
return createEl(
"a",
{
className: "ref",
href: "#" + headingEl.id,
textContent: "#",
title: headingEl.textContent
}
);
};
window.addEventListener("load", function () {
Array.prototype.forEach.call(document.querySelectorAll("#main h1[id], #main h2[id], #main h3[id], #main h4[id], #main h5[id]"), function (el) {
var a = createAnchorFromHeading(el);
el.classList.add("has-ref");
el.addEventListener("click", function () {
a.click();
});
el.insertBefore(a, el.firstChild);
});
});
})();