Skip to content

Instantly share code, notes, and snippets.

@Olein-jp
Created March 4, 2022 03:14
Show Gist options
  • Save Olein-jp/627038b4b1deec313aab03faf63510b7 to your computer and use it in GitHub Desktop.
Save Olein-jp/627038b4b1deec313aab03faf63510b7 to your computer and use it in GitHub Desktop.
自ページへアンカーリンクを持つ要素の親要素に任意のクラス名を付与する
window.onload = function() {
const htmlFileName = window.location.href.split( '/' ).pop();
const fileSlugName = htmlFileName.split( '.' ).shift();
console.log(fileSlugName);
regex = new RegExp( fileSlugName );
const targetElement = document.getElementById( 'column-navigation-content' );
const linkElement = targetElement.getElementsByTagName( 'a' );
const linkElements = Array.from( linkElement );
linkElements.forEach( function (element) {
console.log(element);
if ( element.href.match(regex) ) {
console.log('match!!');
const parentElement = element.parentNode;
parentElement.classList.add('_current');
} else {
console.log('no match');
}
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment