在早期人们用name后来改用id,为了兼容的问题,人们会同时使用name与id,并给与它们相同的值。下面是JS2。0的参考内容。 Before the standardization of HTML 4 and XHTML 1, the name attribute was used to expose items to scripting instead of id. For backward compatibility, the name attribute is commonly defined for <<a>>, <<applet>>, <<button>>, <<embed>>, <<form>>, <<frame>>, <<iframe>>, <<img>>, <<input>>, <<object>>, <<map>>, <<select>>, and <<textarea>>. Notice that the occurrence of the name attribute corresponds closely to the traditional Browser Object Model. Note Both <<meta>> and <<param>> support an attribute called name, but these have totally different meanings unrelated to script access. Page developers must be careful to use name where necessary to ensure backward compatibility with older browsers. Even if this is not a concern to you, readers should not be surprised to find that many modern browsers prefer the name attribute on tags that support it. To be on the safe side, use name and id attributes on the tags that support both and keep them the same value. So we would write<<form name="myForm" id="myForm" method="get" action="#">> <<input type="text" name="userName" id="userName" />> <</form>> And then to access the form from JavaScript, we would use either window.document.myForm or simplydocument.myForm because the Window object can be assumed. The text field would be accessed in a similar fashion by using document.myForm.userName. Note Having matching name and id attribute values when both are defined is a good idea to ensure backward browser compatibility. However, be careful—some tags, particularly radio buttons, must have consistent names but varying id values. See Chapter 14 for examples of this problem.
name 是 HTML 标准中使用的属性.后面微软自己制定的 DHTML 中扩充了 HTML 标准, 并加入了自己的一些属性, 包括 id 等.因此, 用 name 可以操作的用 id 均可以, 但反过来就不行.如 innerHTML ,在HTML标准中没有,故不能用 name 取得元素来操作, 必须要用 id.
而HTML控件都有name属性??
Before the standardization of HTML 4 and XHTML 1, the name attribute was used to expose items to scripting instead of id. For backward compatibility, the name attribute is commonly defined for <<a>>, <<applet>>, <<button>>, <<embed>>, <<form>>, <<frame>>, <<iframe>>, <<img>>, <<input>>, <<object>>, <<map>>, <<select>>, and <<textarea>>. Notice that the occurrence of the name attribute corresponds closely to the traditional Browser Object Model. Note Both <<meta>> and <<param>> support an attribute called name, but these have totally different meanings unrelated to script access.
Page developers must be careful to use name where necessary to ensure backward compatibility with older browsers. Even if this is not a concern to you, readers should not be surprised to find that many modern browsers prefer the name attribute on tags that support it. To be on the safe side, use name and id attributes on the tags that support both and keep them the same value. So we would write<<form name="myForm" id="myForm" method="get" action="#">>
<<input type="text" name="userName" id="userName" />>
<</form>>
And then to access the form from JavaScript, we would use either window.document.myForm
or simplydocument.myForm
because the Window object can be assumed. The text field would be accessed in a similar fashion by using document.myForm.userName. Note Having matching name and id attribute values when both are defined is a good idea to ensure backward browser compatibility. However, be careful—some tags, particularly radio buttons, must have consistent names but varying id values. See Chapter 14 for examples of this problem.
A, APPLET, attribute, BUTTON, EMBED, FORM, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=hidden, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, LINK, MAP, OBJECT, RT, RUBY, SELECT, TEXTAREA下面的有ID,
STYLE, A, ACRONYM, ADDRESS, APPLET, AREA, B, BASE, BASEFONT, BDO, BGSOUND, BIG, BLOCKQUOTE, BODY, BR, BUTTON, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, COMMENT, CUSTOM, DD, DEL, DFN, DIR, DIV, DL, DT, EM, EMBED, FIELDSET, FONT, FORM, FRAME, FRAMESET, HEAD, hn, HR, HTML, I, IFRAME, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=hidden, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, INS, ISINDEX, KBD, LABEL, LEGEND, LI, LINK, LISTING, MAP, MARQUEE, MENU, nextID, NOBR, NOFRAMES, NOSCRIPT, OBJECT, OL, OPTION, P, PLAINTEXT, PRE, Q, RT, RUBY, S, SAMP, SCRIPT, SELECT, SMALL, SPAN, STRIKE, STRONG, styleSheet, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TITLE, TR, TT, U, UL, VAR, WBR, XML, XMP