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>
<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>
相关文章推荐
- 编写js公共函数addclass()实现改变样式
- 原生js获取元素的class属性(获取class的所有元素)以及改变或添加calssName
- 第22天:js改变样式效果
- CSS3或CSS+JS实现改变滚动条样式(兼容所有浏览器)
- 表格,鼠标移到,移出某行,改变该行样式JS
- 一些比较有用的js脚本--通过class改变css样式
- js 中用Dom2级事件处理函数(改变样式)
- JS获取样式 改变样式
- 使用js改变DOM样式的三种方式 - style属性
- js改变style样式和css样式
- 点击button获取text内容并改变样式的js实现
- 通过Class改变单元格样式(cell)
- js改变DOM样式的三种方式
- js实现class样式的修改、添加及删除的方法
- js随笔,css和js禁止网页选择文本,table的class样式使得td的class样式失效,jquery获得元素坐标
- 鼠标滑过改变样式JS函数
- Ferris教程学习笔记:js示例2.4 鼠标移入改变样式,鼠标移出恢复
- vue.js学习笔记之绑定style样式和class列表
- Vue.js每天必学之Class与样式绑定