<script language="javascript" type="text/javascript">
function show(){//定义一个函数
var new_div=document.createElement('div');//创建一个节点每个字符串其实也是一个对象
new_div.style.width='100px';对象调用格式的宽
new_div.style.height='100px';对象调用格式的高
new_div.style.backgroundColor='blue';对象调用的背景颜色
new_div.id='div1';对象的id属性
document.body.appendChild(new_div);确定new_div的位置,通过appendChild讲nnew_div放在body总,实现图层分布在body体内
var text=document.createTextNode('abcd');创建一个文本内容,通过dom的TextNode属性
var test=document.createTextNode('admin');同上
new_div.appendChild(text);上面已经创建了文本的内容,有要通过appenChild放在new_div中,实现图层总填充内容
new_div.appendChild(test);
alert(document.getElementById('div1').nodeName);返回当前节点名称的字符串。
}
</script></head>
<body>
<input type="button" onclick="show()" value="click" />响应show事件
执行代码的效果: