Created
March 4, 2022 03:14
-
-
Save Olein-jp/627038b4b1deec313aab03faf63510b7 to your computer and use it in GitHub Desktop.
自ページへアンカーリンクを持つ要素の親要素に任意のクラス名を付与する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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