【发布时间】:2025-12-11 17:00:01
【问题描述】:
如果 url 是子域 http://one.example.com/dsnknw?211218 则显示 div 1,如果 http://two.example.com/ppppnw?201218 则显示 div 2。
每个身体都有背景图片
<style>
#siteone {
background: url(http://example.info/backgroundone.PNG);
background-repeat: no-repeat;
background-size:cover;
}
#sitetwo {
background: url(http://example.info/backgroundtwo.PNG);
background-repeat: no-repeat;
background-size:cover;
}
</style>
以及相关的链接:
<div id="one">
<a id="link1" href="https://click.com" target="_top"></a>
</div>
<div id="two">
<a id="link2" href="https://click2.com" target="_top"></a>
</div>
我尝试了一些代码,包括
document.getElementById("body")
Class of ID Change based on URL - URL Based Image Swap - Creating Conditional Statement Based On Page URL 等
【问题讨论】:
-
为什么不在你的每一个页面body上放一个class,为每个body.otherClass制定不同的css规则呢?然后根本不需要javascript。
-
有only one body per document。考虑到这一点,您应该改写您的问题。
document.getElementById("body")没有意义,因为您没有将任何东西的 id 设置为“body”。您的意思可能是document.querySelector("body"),或者更好的是document.body。您的问题应该显示您尝试过的、实际与预期的行为错误消息。最好的办法是创建一个简单的可重现示例 -
对我来说整个问题都是背景。它只在 body 中正确显示,而不是在 div 中
标签: javascript if-statement getelementbyid