简体   繁体   English

如果域名=,则更改div的类

[英]change class of a div if domain name =

I have 2 domains going to my site but I want to change the class of a div that has an ID of #logo. 我有2个域要访问我的网站,但我想更改ID为#logo的div的类。

I have tried the following but no luck... 我尝试了以下方法,但是没有运气...

<script type="text/javascript"> 
var hasChar1 = window.location.href.indexOf('domain01') != -1;
var hasChar2 = window.location.href.indexOf('domain') != -1;
if (hasChar1)
{
   if (hasChar1)
   {document.getElementById("logo").className = "logoNZ";}
   else
   {document.getElementById("logo").className = "";}
}
else if (hasChar2)
{document.getElementById("logo").className = "";}
</script>

Why inserting the same if statement inside itself? 为什么要在自身内部插入相同的if语句?

This should work: 这应该工作:

document.getElementById('logo').className = (window.location.hostname.indexOf('domain') !== -1) ? 'logoNZ' : 'notLogoNZ';

Make sure you place this code at the end of your body, after the div as already been rendered! 确保将这段代码放在已渲染的div之后的正文末尾!

Have you tried something like this ? 你尝试过这样的事情吗?

if (window.location.hostname == "domain01"){
    document.getElementById("logo").className = "logoNZ";
}
else if (window.location.hostname == "domain"){
    document.getElementById("logo").className = "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM