动态创建HTML元素并添加属性
2018-09-06 本文已影响9人
puxiaotaoc
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>柴婷的测试</title>
<style>
.my-class{
color:red;
}
.p-class{
color:green;
font-size: 34px;
}
</style>
</head>
<body>
<script type="text/javascript">
var d = document.createElement('div');
d.setAttribute('class','my-class');
var p = document.createElement('p');
p.setAttribute('class','p-class');
var text = document.createTextNode('我是p标签')
p.appendChild(text);
d.appendChild(p);
document.body.appendChild(d);
</script>
</body>
</html>
动态创建HTML元素并添加属性