先看這樣一段 html
<form name="fName" id="fId">
<input name="name" >
<input name="pwd" type="password" >
</form>
如何得到 form 標籤的引用呢?
- 根據 id
document.getElementById('fId');
- 根據 name
document.fName
如何得到 form 裏面的 input 呢?
// 先拿到 form 標籤
var form = document.fName;
// 要得到 密碼框 這個文本框
// 一種做法
var pwd = form.querySelectorAll('input[name=pwd]');
// 更簡單的方法
pwd = form.pwd;