案例:javascript案例     状态:可编辑再运行    进入竖版
 运行结果 
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<style type="text/css">
5
.democlass{
6
    color:red;
7
}
8
</style>
9
</head>
10
<body>
11
12
<h1>Hello World</h1>
13
14
<p id="demo">请点击按钮,将 H1 的 class 属性设置为 "democlass"。</p>
15
16
<button onclick="myFunction()">试一下</button>
17
18
<script>
19
function myFunction()
20
{
21
var h=document.getElementsByTagName("H1")[0];
22
var typ=document.createAttribute("class");
23
typ.nodeValue="democlass";
24
h.attributes.setNamedItem(typ);
25
}
26
</script>
27
28
</body>
29
</html>
30