1.方式
通过 . 的方式获取和读写
2.程序
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 <script type="text/javascript"> 7 window.onload=function(){ 8 var userNode=document.getElementById("n1"); 9 //获取值 10 alert("value: "+userNode.value); 11 //设置值 12 userNode.value="javaScript"; 13 } 14 </script> 15 </head> 16 <body> 17 名称:<input type="text" id="n1" name="user" value="js"/> 18 </body> 19 </html>
3.运行结果
4.衍生程序
需要理解属性节点的意思,这样才会知道如何获取属性的值,如何给属性节点赋值。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 <script type="text/javascript"> 7 window.onload=function(){ 8 var userNode=document.getElementById("n1"); 9 //获取值 10 alert("value: "+userNode.value); //js 11 alert("name: "+userNode.name); //user 12 alert("value: "+userNode.id); //text 13 //设置值 14 userNode.value="javaScript"; 15 } 16 </script> 17 </head> 18 <body> 19 名称:<input type="text" id="n1" name="user" value="js"/> 20 </body> 21 </html>