zl程序教程

您现在的位置是:首页 >  其它

当前栏目

不通的页面,点击导航改变背景色--(代码优化后)

-- 页面 改变 点击 导航 背景色 不通 代码优化
2023-09-11 14:14:50 时间
<script>
var cid=2;//获取不通页面的ID

switch(cid)
{
  case 1:
    cadd("#c1");
    break;
  case 2:
    cadd("#c2");
    break;
  case 3:
    cadd("#c3");
    break;
}



function cadd(cadd_number){
$(cadd_number).addClass("cadd");
}
</script>

以上是JS代码

<div class="" id="c1">哈哈</div>
<div class="" id="c2">oo</div>
<div class="" id="c3">hh </div>
以上是HTML代码

压缩包下载: http://download.csdn.net/detail/cplvfx/9793636 只需要1个积分

代码优化后

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>不通的页面,导航色改变</title>
<script type="text/javascript" src="http://www.w3school.com.cn/jquery/jquery.js"></script>
<style>
.cadd{ background:red; color:#FFFFFF;}
</style>
</head>

<body>
<div class="" id="c1">哈哈</div>
<div class="" id="c2">oo</div>
<div class="" id="c3">hh </div>
<script>
var cid=1;//获取是那个id
for(var i=0;i<=cid;i++){
if(i==cid){
cadd("#c"+i);
}
}

//switch(cid)
//{
// case 1:
//    cadd("#c1");
//    break;
//  case 2:
//    cadd("#c2");
//    break;
//  case 3:
//    cadd("#c3");
//    break;
//}

function cadd(cadd_number){
$(cadd_number).addClass("cadd");
}
</script>
</body>
</html>