Strict doctype - form and input element

本秂侑毒 提交于 2019-12-22 05:13:11

问题


Does anyone know the reasoning behind the strict doctype not allowing input elements to be direct descendents of a form element. I find it annoying that i have to wrap a submit button which is a block level element inside another block level element say a fieldset or a div. However, I cannot find an answer anywhere as to why this actually is.


回答1:


So if you try to put an input directly into a form without a container element, and validate under xhtml 1.0 strict, you get this warning:

document type does not allow element "input" here; missing one of "p", "h1", "h2", "h3", "h4", "h5", "h6", "div", "pre", "address", "fieldset", "ins", "del" start-tag ✉ The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.

One possible cause for this message is that you have attempted to put a block-level element (such as "<p>" or "<table>") inside an inline element (such as "<a>", "<span>", or "<font>").

And if you look here at the W3C definition of a form element (http://www.w3.org/TR/html4/interact/forms.html#h-17.3) you can see that that the element's content model is defined as "%block".

<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form -->

If you follow the "%block" link (http://www.w3.org/TR/html4/sgml/dtd.html#block) that leads you to the elements that are defined as those types of elements. And those are:

<!ENTITY % block
     "P | %heading; | %list; | %preformatted; | DL | DIV | NOSCRIPT |
      BLOCKQUOTE | FORM | HR | TABLE | FIELDSET | ADDRESS">

So, as you can see, the W3C doesn't define an input or a button as a block level element. You can search that page for "input" and find that it's content type of "formctrl":

<!ENTITY % formctrl "INPUT | SELECT | TEXTAREA | LABEL | BUTTON">

And, really, input elements default display as more inline-block than block, considering they don't cause line breaks before/after them. So, there are more than just inline elements and block-level elements.

So, in the end, a form needs it's direct children to be block level elements, and input elements don't qualify. I hope that clears everything up.



来源:https://stackoverflow.com/questions/2982590/strict-doctype-form-and-input-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!