HTML attribute  --> DOM property

每个html属性都对应一个DOM对象属性,举个栗子:

<div>
    <label for="userName">用户名:</label>
    <input id="userName" type="text"  class="u-txt" />
</div>
HTMLDOM
input.idinput.idinput.typeinput.typeinput.classinput.classNamelabel.forlabel.htmlFor

读

input.className  // “u-txt”

input[“id”]    // ”userName”

写

input.value=’[email protected]’;

类型

input.

属性名返回值返回值类型
className“u-txt”StringmaxLength10NumberdisabledtrueBooleanonclickfunction onclick(event){ … }Function

✘通用性--名字异常:  由于一些属性名与关键字重合,所以不能直接使用属性名访问,如class就会变成className

✘ 扩展性

✔实用对象

g/setAttribute

读

var attribute=element.getAttribute(attributeName);

<div>
    <label for="userName">用户名:</label>
    <input id="userName" type="text" class="u-txt">
</div>
input.getAttribute("class");   //"u-txt"

写

element.setAttribute(name,value);

<div>
    <label for="userName">用户名:</label>
    <input id="userName" type="text" class="u-txt">
</div>
input.setAttribute("value","[email protected]");
input.setAttribute("disabled",""); //在html中,布尔型的属性只要出现默认是true

类型

<input class="u-txt"
        maxlength="10"
        disabled
        onclick="showSuggest();">

input.getAttribute(“ 属性字符串

属性名返回值返回值类型
class“u-txt”Stringmaxlength“10”Stringdisabled“”Stringonclick“showSuggest();”String

”)

举个栗子:

button.disabled = true;
button.setAttribute('class','disabled');