DOM通过ID或NAME获取值
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>通过ID获取值</title> <script type="text/javascript"> var getname = function () { alert( document.getElementById('name').value); } var getage = function () { alert(document.getElementById('age').value); }
<script type="text/javascript">
function givevalue()//用来测验getElementsByTagName();
{
var inputs = document.getElementsByTagName("input");
for(var i=0;i<inputs.length;i++)
{
inputs[i].value = "Nihao";
}
}
</script>
function getname1() { var txt = document.getElementById("name"); alert(txt.value); } function getage1() { alert(document.getElementById('age').value); } </script> <script type="text/javascript"> function Getelemntbyname() { var rads = document.getElementsByName("gender"); for(var i=0;i<rads.length;i++) { var ra = rads[i]; alert(ra.value); } } </script> </head> <body> <input type="text" id="name" /> <input type="button" value="点击"onclick="getname()"/><br/> <input type="text" id="age" /> <input type="button" value="点击" onclick="getage()"/> <input type="radio" name="gender" value="男"/>男 <input type="radio" name="gender" value="女" />女 <input type="radio" name="gender"value="保密" />保密 <input type="radio" name="gender" value="变异"/>变异 <input type="button" onclick=" Getelemntbyname()" value="click"/> </body> </html>
通过GetElementByID();进行操作按钮倒计时的案例
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <title>通过ID获取值</title> 5 6 <script type="text/javascript"> 7 var TotalLefttime = 10; 8 var Interval;//设置的全局变量 9 function countDown() 10 { 11 var btnReg = document.getElementById('btnreg'); 12 13 if(btnReg) 14 { 15 if(TotalLefttime<=0) 16 { 17 btnreg.value = "同意"; 18 btnreg.disabled = ""; 19 clearInterval(Interval);//停止继续进行这个过程 20 } 21 else 22 { 23 btnreg.value = "请仔细阅读协议,还剩" + TotalLefttime + "秒"; 24 TotalLefttime--; 25 } 26 } 27 } 28 Interval = setInterval("countDown()",1000);//启动定时器 29 </script> 30 </head> 31 <body onload="intevent()"> 32 <textarea> 33 阅读这里的内容。 34 </textarea> 35 36 <input type="button" id="btnreg" disabled="disabled" /> 37 </body> 38 </html> 39