您的位置:首页 > Web前端 > CSS

js改变class的样式

2008-09-08 18:43 357 查看
一般js可以通过id来改变一个object的属性和样式,但是id在一个页面中是唯一的,当想改变一批object时就无能为力了,但是class可以设置多个,这样就可以通过js设置 <div class="title"> <div>里的样式

<html>

<head>

<title></title>

</head>

<style>

.title

{

background:#0ff; width:150px; height:150px;

}

.title_other

{

background:#ff0; width:150px; height:150px;

}

</style>

<script>

function changeCss()

{

var arr = document.getElementsByTagName('div');

var num = arr.length;

for(var i=0;i<num;++i)

{

if(arr[i].className == "title")

{

arr[i].className = "title_other";

}

}

}

</script>

<body>

<div class="title" onmouseover="changeCss()">变化1</div>

<div class="title" onmouseover="changeCss()">变化2</div>

<div class="title" onmouseover="changeCss()">变化3</div>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: