注册 登录
编程论坛 JavaScript论坛

求助javascript脚本代码,根据url条件判断,控制div层的显示和隐藏

aoxiao007 发布于 2009-08-28 23:41, 1167 次点击
首页和栏目页用的是相同的网页模板,为了区分功能,想通过javascript 实现这样的功能:
当打开首页. 把id="box1" 的DIV层自动隐藏.
当url不是首页地址的时候, 把id="box2"的DIV层隐藏.
例如.当url为http://www. 时,自动隐藏<DIV id="box1"></DIV> 标签内的页面内容.<DIV id="box2"></DIV> 正常显示.
当url为http://www.等,自动隐藏<DIV id="box2"></DIV> 标签内的页面内容.<DIV id="box1"></DIV> 正常显示.
   下面的代码,不知道那里出错了,结果就是出不来!!!
<html>
<body>
<div id="box1">
box1
</div>
<div id="box2">
box2
</div>
<script type="text/javascript">
function disPlay(){
  var b1 = document.getElementById("box1");
  var b2 = document.getElementById("box2");
  if(window.location.href = "http://www.){
      b1.style.display = "none";
      b2.style.display = "block"
  }
  else{
      b1.style.display = "block";
      b2.style.display = "none"
  }

}
</script>
</body>
</html>

[ 本帖最后由 aoxiao007 于 2009-8-29 11:19 编辑 ]
3 回复
#2
簡單就好2009-09-05 11:15
我觉得应该是这句if(window.location.href = http://www.)有错其中应该是==而不是=后者是赋值前者才是判断是否相等 不知对否
#3
骨灰菜鸟2009-09-07 10:26
if(window.location.href = http://www.)
应该是if(window.location.href == "http://www.)吧,你那是赋值语句
#4
bc_dmy20082009-09-09 23:15
<html>
<body>
<div id="box1">
box1
</div>
<div id="box2">
box2
</div>
<script language="JavaScript">
function disPlay(){
  var b1 = document.getElementById("box1");
  var b2 = document.getElementById("box2");
  if(window.location.href = "http://www.)
      b1.style.display = "none";
      b2.style.display = "block";
   
  else
      b1.style.display = "block";
      b2.style.display = "none";
   
 
}
</script>
</body>
</html>
1