52,780
社区成员
发帖
与我相关
我的任务
分享<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>addClass.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
.redCls {
color: red;
}
.borderCls {
border: 1px solid red;
}
.basicCls {
width: 100px;
height: 20px;
}
</style>
<script type="text/javascript">
window.onload = function () {
var boxs = document.getElementsByTagName("div");
for (var i = 0; i < boxs.length; i++) {
alert(boxs[i].className);
boxs[i].className += " redCls";
if (i == 1) {
boxs[i].className += " borderCls";
}
}
};
</script>
</head>
<body>
<div class="basicCls">aaa</div>
<div class="basicCls">bbb</div>
<div class="basicCls">ccc</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JQuery添加移除Css样式</title>
<script language="javascript" src="/Script/jquery-1.4.2.js"></script>
<style type="text/css">
.div{height:20px; margin-top:10px;}
.div1{border:1px solid red;}
.div2{border:1px solid #ccc;}
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
$(document).ready(function(){
// $("div").addClass("div");//给所有的层都添加div的样式
$("#div1").addClass("div1");//给div1 添加样式div1
$("#div2").addClass("div2");//给div2 添加样式div2
$("#div1").click(function(){
$(this).addClass("div");//移除div1的样式
})
$("#div2").click(function(){
$(this).addClass("div");//移除div2的样式
})
})
//-->
</SCRIPT>
</head>
<body>
<div id="div1">div1</div>
<div id="div2">div2</div>
</body>
</html>
var sp = document.getElementById("spanId");
sp.className += " newClass";//注意要多加一个空格