1. HTML里面的name, id, class有何區(qū)別?
1.1 name
1.1.1 格式:
<input type="text" name="username" />
<input type="radio" name='sex'/>男
<input type="radio" name='sex'/>女
function changtxtcolor() {
var txts = document.getElementsByName('txtcolor'); //獲取所有name=txtcolor 的標簽
for (var i = 0; i < txts.length; i++) { //循環(huán)遍歷標簽,并把背景色改為red
txts[i].style.backgroundColor = 'red';
}
}
1.1.2 應(yīng)用:
- form表單:name可作為轉(zhuǎn)遞給服務(wù)器表單列表的變量名;如上面的傳到服務(wù)器的名稱為:username='text的值'。
- input type='radio'單選標簽:把幾個單選標簽的 name設(shè)為一個相同值時,將會進行單選操作。
- 快速獲取一組name相同的標簽:獲取擁有相同name的標簽,一起進行操作,如:更改屬性、注冊事件等。
1.1.3 特性:
name屬性的值,在當前page頁面中并非唯一性。
1.2 id
1.2.1 格式:
<input type=password id="userpwd" />
1.2.2 應(yīng)用:
- 根據(jù)提供的唯一id號,快速獲取標簽對象。如:document.getElementById(id)
- 用于充當label標簽for屬性的值:示例:<label for='userid'>用戶名:</label>,表示單擊此label標簽時,id為userid的標簽獲得焦點。
1.2.3 特性:
id屬性的值,在當前的page頁面要是唯一的。
1.3 class
1.3.1 格式:
<input type=button class="btnsubmit" />
1.3.2 應(yīng)用:
CSS操作,把一些特定樣式放到一個class類中,需要此樣式的標簽,可以在添加此類。
1.3.3 特性:
可以把多個類,放在一個class屬性里,但必須用空格隔開;如:class='btnsubmit btnopen'