location.pathname;でhoge.htmlのid、areaに対してクラスを追加したんですがうまくいきません。if文がない場合は追加されるのですが、全ページにもクラスがついてしまうので
このhoge.htmlだけにつけたいのです
レクチャー頂ければ幸いです。
・追加できない window.onload = function () { let url = location.pathname; if (url == "hoge.html") { const targetElement = document.getElementById('area'); targetElement.classList.add('none'); } } 追加できる window.onload = function () { let url = location.pathname; document.getElementById("area").innerHTML = location.pathname; } }
hoge.html を表示しているとき、location.pathname には何が入っているか確認しましたか。

回答2件
あなたの回答
tips
プレビュー