perfect: improve browser compatibility

This commit is contained in:
jeffreytse 2020-10-03 12:23:29 +08:00
parent ba1db7db3e
commit 023ba51a08
3 changed files with 15 additions and 5 deletions

View file

@ -36,7 +36,7 @@
// The first event occurred // The first event occurred
window.addEventListener('click', function(event) { window.addEventListener('click', function(event) {
if (event.target.matches('a')) { if (event.target.tagName.toLowerCase() == 'a') {
hashLocate(event.target.getAttribute('href')); hashLocate(event.target.getAttribute('href'));
} }
}); });

View file

@ -36,9 +36,13 @@
// The header element // The header element
var header = document.querySelector('header.site-header'); var header = document.querySelector('header.site-header');
function doMenuCollapse(index, over_items=20) { function doMenuCollapse(index, over_items) {
var items = menuContent.firstChild.children; var items = menuContent.firstChild.children;
if (over_items == undefined) {
over_items = 20;
}
if (items.length < over_items) { if (items.length < over_items) {
return; return;
} }

View file

@ -58,10 +58,12 @@ function smoothScrollTo(y, time) {
// Init highlight js // Init highlight js
document.addEventListener('DOMContentLoaded', function(event) { document.addEventListener('DOMContentLoaded', function(event) {
document.querySelectorAll('pre code').forEach((block) => { var els = document.querySelectorAll('pre code')
function handle(block) {
var outer = block.parentElement.parentElement.parentElement; var outer = block.parentElement.parentElement.parentElement;
var lang = block.getAttribute('data-lang'); var lang = block.getAttribute('data-lang');
for (var cls of outer.classList) { for (var i = 0; i < outer.classList.length; i++) {
var cls = outer.classList[i];
if (cls.startsWith('language-')) { if (cls.startsWith('language-')) {
lang = cls; lang = cls;
break; break;
@ -77,5 +79,9 @@ document.addEventListener('DOMContentLoaded', function(event) {
block.setAttribute('class', 'hljs ' + lang); block.setAttribute('class', 'hljs ' + lang);
block.parentNode.setAttribute('data-lang', lang); block.parentNode.setAttribute('data-lang', lang);
hljs.highlightBlock(block); hljs.highlightBlock(block);
}); }
for (var i = 0; i < els.length; i++) {
var el = els[i];
handle(el);
}
}); });