我试图仅在主页上的标题中隐藏此类“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';
}
欢迎任何在主页上隐藏 CSS 类的建议
最佳答案
document
中没有url
属性,但是有一个URL
。
因此您可以使用 document.URL
或 window.location.href
。
我过去在使用 document.URL
时遇到过问题,所以更喜欢第二个。
编辑:请注意,如果您的 URL 包含哈希(例如:comparer-acheter.fr/#hash
),此解决方案将不起作用。如果您的网站名称更改,它也将不起作用。如果你想检查你是否在主页上,你可以使用这个:
if(window.location.pathname == '/') {
// you are on the homepage
}
您将在这篇文章中获得有关locations
如何工作的更多信息:javascript window location href without hash?
关于javascript - 仅在主页上隐藏 CSS 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50408785/