仅在主页上隐藏CSS类

1 javascript css hide website-homepage

我试图在HomePage上隐藏这个类"mediad rh-flex-right-align".此类必须出现在每个其他页面上.

我尝试使用Javascript来实现这一点,但它没有工作,所以我可能在这些方面犯了一个错误:

if (document.url == "https://www.comparer-acheter.fr/")
{
document.getElementsByClassName("mediad rh-flex-right-align")
[0].style.display = 'none';
}
Run Code Online (Sandbox Code Playgroud)

欢迎任何建议隐藏主页上的CSS类

Eba*_*sin 5

没有url属性,document但有一个属性URL.

所以你可以使用document.URLwindow.location.href.

document.URL在过去遇到过问题,所以更喜欢第二个问题.

编辑:请注意,如果您的网址包含哈希值,则此解决方案将无效(例如:)comparer-acheter.fr/#hash.如果您网站的名称发生变化,它也将无效.如果您想检查自己是否在主页上,可以使用以下命令:

if(window.location.pathname == '/') {
    // you are on the homepage
}
Run Code Online (Sandbox Code Playgroud)

你将有关于如何locations在这篇文章上工作的更多信息:没有哈希的javascript窗口位置href?