[Copy to clipboard]
<html> 这个例子就是通过Javascript控制DOM,在页里面写内容还有就是对页面内容用CSS修饰。 <head> <title>Hello.the World</title> <style type="text/css"><!-- .declared { color:red; font-size:14px; } .programmed { color:blue; font-size:12px; font-weight:bold;} --></style> <script> window.onload=function() { var hello=document.getElementById('hello'); //通过ID找到元素 hello.className='declared'; var empty=document.getElementById('empty'); addNode(empty,"reader of"); addNode(empty,"Ajax in Action!"); var children=empty.childNodes; for (var i=0;i<children.length;i++) { children[i].className='programmed'; } empty.style.border='solid green 2px'; //直接为节点设置样式 empty.style.width="200px"; } function addNode(el,text){ var childEl=document.createElement("div"); //创建新元素 el.appendChild(childEl); var txtNode=document.createTextNode(text); //创建文本元素 childEl.appendChild(txtNode); } </script> </head> <body> <p id="hello">Hello</p> <div id="empty"></div> </body> </html> |