指定標籤的名稱。
格式
<input type=text name=username />
應用場景
①form表單:name可作為轉遞給服務器表單列表的變量名;如上面的傳到服務器的名稱為:username='text的值'。
②input type='radio'單選標籤:把幾個單選標籤的name設為一個相同值時,將會進行單選操作。
<input type=radio name='sex'/>男<input type=radio name='sex'/>女
③快速獲取一組name相同的標籤:獲取擁有相同name的標籤,一起進行操作,如:更改屬性、註冊事件等。
function changtxtcolor() { var txts = document.getElementsByName('txtcolor'); //獲取所有name=txtcolor 的標籤for (var i = 0; i < txts.length; i++) { //循環遍歷標籤,並把背景色改為red txts[i].style.backgroundColor = 'red'; }}特性
name屬性的值,在當前page頁面中並非唯一性。
id/256798.html">id指定標籤的唯一標識。
格式
<input type=password id=userpwd />
應用場景
①根據提供的唯一id號,快速獲取標籤對象。如:document.getElementById(id)
②用於充當label標籤for屬性的值:示例:<label for='userid'>用戶名:</label>,表示單擊此label標籤時,id為userid的標籤獲得焦點。
特性
id屬性的值,在當前的page頁面要是唯一的。
class指定標籤的類名。
格式
<input type=button class=btnsubmit />
應用場景
①CSS操作,把一些特定樣式放到一個class類中,需要此樣式的標籤,可以在添加此類。
特性
可以把多個類,放在一個class屬性裡,但必須用空格隔開;如:class='btnsubmit btnopen'