Javascript 在结果页上隐藏CSS类

Javascript 在结果页上隐藏CSS类,javascript,css,Javascript,Css,我试图仅在页面结果的标题中隐藏这个类“mediad rh flex right align”。这个类必须出现在除主页之外的每一个页面上,在主页上我已经实现了一个javascript方法来隐藏它 下面是从主页中隐藏它的代码: <script> if (window.location.href == "https://www.comparer-acheter.fr/"){ document.getElementsByClassName("rh-flex-left-alig

我试图仅在页面结果的标题中隐藏这个类“mediad rh flex right align”。这个类必须出现在除主页之外的每一个页面上,在主页上我已经实现了一个javascript方法来隐藏它

下面是从主页中隐藏它的代码:

<script>
        if (window.location.href == "https://www.comparer-acheter.fr/"){
document.getElementsByClassName("rh-flex-left-align")[0].style.display = 'none';
}
</script>

如果(window.location.href==”https://www.comparer-acheter.fr/"){
document.getElementsByClassName(“rh flex left align”)[0]。style.display='none';
}
我正在搜索代码以隐藏该CSS类,以及所有获得该路径的页面(/product search”)(例如):


如果(window.location.href==”https://www.comparer-acheter.fr/")
如果(window.location.href==”https://www.comparer-acheter.fr/product-search/") {
document.getElementsByClassName(“rh flex left align”)[0]。style.display='none';
}
显然我没有使用正确的代码。。 很高兴听到你能帮忙

Flo

使用以下方法:

if(location.href.split(location.host)[1].startsWith("/product-search")) {
    // set to display none
    document.getElementsByClassName("rh-flex-left-align")[0].style.display = 'none';
}

您可能需要使用window.location.href…

如果您想针对单个元素,为什么不给它一个id?抓住所有元素并针对第一个元素似乎是一种猜测-worky@DMcCallum83使用cssid可能是一个很好的选择,但是所有页面都有一个公共头(一个PHP文件)啊,我明白了。我现在明白了。在第二段代码中,你是怎么想的?你是对的,在第二段代码中使用了两个if语句?@DMcCallum83-Hum,你是对的。这里可能更符合逻辑。我对javascript很糟糕
if(location.href.split(location.host)[1].startsWith("/product-search")) {
    // set to display none
    document.getElementsByClassName("rh-flex-left-align")[0].style.display = 'none';
}