您好,欢迎访问一九零五行业门户网

javascript DOM实用学习资料

访问指定节点:
getelementsbyname():
<html> <head> <title>dom技术</title> </head> <body> <form method="post" action="document.cgi"> <fieldset> <legend>选择你喜欢的颜色!</legend> <input type="radio" name="color" value="red"/>red <input type="radio" name="color" value="green"/>green <input type="radio" name="color" value="blue"/>blue </fieldset> <input type="submit" value="submit"> </form> <script language="javascript"> var oradios=document.getelementsbyname("color"); alert(oradios[0].getattribute("value")); </script> </body> </html>
<html> <head> <title>dom技术</title> </head> <body> <form method="post" action="document.cgi"> <fieldset> <legend>选择你喜欢的颜色!</legend> <input type="radio" name="color" value="red"/>red <input type="radio" name="color" value="green"/>green <input type="radio" name="color" value="blue"/>blue </fieldset> <input type="submit" value="submit"> </form> <script language="javascript"> var oradios=document.getelementsbyname("color"); alert(oradios[0].getattribute("value")); </script> </body> </html>
getelementbyid():
<html> <head> <title> </title> <script type="text/javascript"> function getvalue(){ var odiv1=document.getelementbyid("div1"); odiv1.innertext="hello!"; } </script> </head> <body onload="getvalue()"> <div id="div1"></div> </body> </html>
<html> <head> <title> </title> <script type="text/javascript"> function getvalue(){ var odiv1=document.getelementbyid("div1"); odiv1.innertext="hello!"; } </script> </head> <body onload="getvalue()"> <div id="div1"></div> </body> </html>
createelement():
<html> <head> <title>创建节点</title> </head> <body onload="createm()"> </body> </html> <script language="javascript"> function createm(){ var op=document.createelement("p"); var otext=document.createtextnode("你好!"); op.appendchild(otext); document.body.appendchild(op); } </script>
<html> <head> <title>创建节点</title> </head> <body onload="createm()"> </body> </html> <script language="javascript"> function createm(){ var op=document.createelement("p"); var otext=document.createtextnode("你好!"); op.appendchild(otext); document.body.appendchild(op); } </script>
removechild():
<html> <head> <title>删除节点</title> <script language="javascript"> function removem(){ var op=document.body.getelementsbytagname("p")[0]; document.body.removechild(op); } </script> </head> <body onload="removem()"> <p>你好!</p> <p>hello world!</p> </body> </html>
<html> <head> <title>删除节点</title> <script language="javascript"> function removem(){ var op=document.body.getelementsbytagname("p")[0]; document.body.removechild(op); } </script> </head> <body onload="removem()"> <p>你好!</p> <p>hello world!</p> </body> </html>
replacechild():
<html> <head> <title>替换节点</title> <script language="javascript"> function appendm(){ var newp=document.createelement("p"); var newtext=document.createtextnode("hello sansan!"); newp.appendchild(newtext); document.body.appendchild(newp); } </script> </head> <body onload="appendm()"> <p>你好!</p> <p>hello world!</p> </body> </html>
<html> <head> <title>替换节点</title> <script language="javascript"> function appendm(){ var newp=document.createelement("p"); var newtext=document.createtextnode("hello sansan!"); newp.appendchild(newtext); document.body.appendchild(newp); } </script> </head> <body onload="appendm()"> <p>你好!</p> <p>hello world!</p> </body> </html>
insertbefore():
<html> <head> <title>新消息出现在旧消息之前</title> <script language="javascript"> function appendm(){ var newp=document.createelement("p"); var newtext=document.createtextnode("hello sansan!"); newp.appendchild(newtext); var oldp=document.getelementsbytagname("p")[0]; document.body.insertbefore(newp,oldp); } </script> </head> <body onload="appendm()"> <p>你好!</p> <p>hello world!</p> </body> </html>
<html> <head> <title>新消息出现在旧消息之前</title> <script language="javascript"> function appendm(){ var newp=document.createelement("p"); var newtext=document.createtextnode("hello sansan!"); newp.appendchild(newtext); var oldp=document.getelementsbytagname("p")[0]; document.body.insertbefore(newp,oldp); } </script> </head> <body onload="appendm()"> <p>你好!</p> <p>hello world!</p> </body> </html>
createdocumentfragment():
原方法:
<html> <head> <title>原方法</title> <script language="javascript"> function oldm(){ var arrtext=["first","second","third","fourth","fifth", "sixth","seventh","eighth","ninth","tenth"]; for(var i=0;i<arrtext.length;i++){ var op=document.createelement("p"); var otext=document.createtextnode(arrtext[i]); op.appendchild(otext); document.body.appendchild(op); } } </script> </head> <body onload="oldm()"> </body> </html>
<html> <head> <title>原方法</title> <script language="javascript"> function oldm(){ var arrtext=["first","second","third","fourth","fifth", "sixth","seventh","eighth","ninth","tenth"]; for(var i=0;i<arrtext.length;i++){ var op=document.createelement("p"); var otext=document.createtextnode(arrtext[i]); op.appendchild(otext); document.body.appendchild(op); } } </script> </head> <body onload="oldm()"> </body> </html>
现方法:
<html> <head> <title>原方法</title> <script language="javascript"> function oldm(){ var arrtext=["first","second","third","fourth","fifth", "sixth","seventh","eighth","ninth","tenth"]; var ofragment=document.createdocumentfragment()//创建文档碎片 for(var i=0;i<arrtext.length;i++){ var op=document.createelement("p"); var otext=document.createtextnode(arrtext[i]); op.appendchild(otext); ofragment.appendchild(op) } document.body.appendchild(ofragment); } </script> </head> <body onload="oldm()"> </body> </html>
<html> <head> <title>原方法</title> <script language="javascript"> function oldm(){ var arrtext=["first","second","third","fourth","fifth", "sixth","seventh","eighth","ninth","tenth"]; var ofragment=document.createdocumentfragment()//创建文档碎片 for(var i=0;i<arrtext.length;i++){ var op=document.createelement("p"); var otext=document.createtextnode(arrtext[i]); op.appendchild(otext); ofragment.appendchild(op) } document.body.appendchild(ofragment); } </script> </head> <body onload="oldm()"> </body> </html>
innertext/innerhtml:
<html> <head> <title> </title> <script type="text/javascript"> function getbackgroundcolor(){ var odiv1=document.getelementbyid("div1"); //odiv1.innertext="<h1>new word </h1>"; odiv1.innerhtml="<h1>new word </h1>"; } </script> </head> <body> <div id="div1"></div> <input type="button" value="getvalue" onclick="getbackgroundcolor()"> </body> </html>
<html> <head> <title> </title> <script type="text/javascript"> function getbackgroundcolor(){ var odiv1=document.getelementbyid("div1"); //odiv1.innertext="<h1>new word </h1>"; odiv1.innerhtml="<h1>new word </h1>"; } </script> </head> <body> <div id="div1"></div> <input type="button" value="getvalue" onclick="getbackgroundcolor()"> </body> </html>
div相当于一个容器,通过innertext或innerhtml向其中嵌入网页内容
以上就是javascript dom实用学习资料_javascript技巧dom技术dom技术创建节点创建节点删除节点删除节点替换节点替换节点新消息出现在旧消息之前新消息出现在旧消息之前原方法原方法原方法原方法的内容。
其它类似信息

推荐信息