为什么会<textarea col="5" row="5" />
在我的现代浏览器中破坏我的布局,但如果我明确使用结束标签,例如<textarea col="5" row="5" /></textarea>
?这一直困扰着我。它是标准的一部分吗?
2 回答
是的,它是标准的一部分:
http://www.w3.org/TR/html401/interact/forms.html#h-17.7
17.7 The TEXTAREA element
<!ELEMENT TEXTAREA - - (#PCDATA) -- multi-line text field -->
<!ATTLIST TEXTAREA
%attrs; -- %coreattrs, %i18n, %events --
name CDATA #IMPLIED
rows NUMBER #REQUIRED
cols NUMBER #REQUIRED
disabled (disabled) #IMPLIED -- unavailable in this context --
readonly (readonly) #IMPLIED
tabindex NUMBER #IMPLIED -- position in tabbing order --
accesskey %Character; #IMPLIED -- accessibility key character --
onfocus %Script; #IMPLIED -- the element got the focus --
onblur %Script; #IMPLIED -- the element lost the focus --
onselect %Script; #IMPLIED -- some text was selected --
onchange %Script; #IMPLIED -- the element value was changed --
>
开始标签:必填,结束标签:必填
为什么它会中断是另一个完全不同的故事,浏览器也可以实现它,这样它只会创建一个空textarea
而不中断。但由于这是标准要求的,你应该遵循它。
- 编辑 -
同样根据@Seth 评论(谢谢!),它没有value
属性,因此初始值位于标签内。
By HTML 4.01 rules, <textarea col="5" row="5" />
is equivalent to <textarea col="5" row="5">>
(making the >
a data character). No browser follows this rule, though. Validators do, but this is difficult to see directly. A long explanation: Empty elements in SGML, HTML, XML, and XHTML.
What browsers do in practice is that they just skip the /
as garbage. This, too, means that you have just a start tag, and the browser then tries to process subsequent characters as contents of textarea
, though it may stop this at some point. Depending on the context, the layout of the document may get messed up.
However, if the document is sent with an XML media type (in HTTP headers), it will be processed by XML rules, and then <textarea col="5" row="5" />
is equivalent to <textarea col="5" row="5"></textarea>
. Such usage is not recommended in XHTML when used on the Internet; see clause C.3 of the (in)famous appendix C of the XHTML 1.0 spec.