1)操纵标签style属性

基本语法

标签对像.style.样式名 = 值;

功能

用于设置标签的某个样式

示例

标签添加样式 var obj1 = document.getElementById(“div1”); obj1.style.width = “1000px”; obj1.style.height = “120px”; obj1.style.backgroundColor = “orange”; obj1.style.width = “1000px”; 给div添加宽度为1000像素 obj1.style.height = “120px”; 给div添加高度为120像素 obj1.style.backgroundColor = “orange”; 给div添加背景色为桔色

2)操纵标签class属性

基本语法

标签对像.className = 类名;

功能

用于设置标签的样式名。

示例

  1. 给<div>标签添加样式名
  2. var obj1 = document.getElementById("div1");
  3. obj1.className = "div1";
  4. obj1.className = "div1";
  5. div 添加样式名为div1

3)this关键字

基本语法

this.className = 类名;
this.style.样式名 = 值;

功能

this 表示当前对象的一个引用。

示例

给div标签添加click事件              
obj1.onclick = function(){     
  this.className = "div1";     
  this.style.color = "white";   
}     
obj1.onclick = function()  元素对象添加点击事件     
this.className = "div1";  给当前元素添加类名为div1     
this.style.color = "white";  给当前元素添加文本颜色样式为白色