一般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>