Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

html - Why is an input tag not allowed directly within a form tag?

I just read the following at http://w3fools.com/#html_forms:

Non-block-level elements (such as <input>) are not valid directly inside <form> tags until HTML5.

I had never heard of anything along these lines, and every basic HTML tutorial I've seen seems to be just fine with putting input tags directly inside a form tag. So my question has three parts:

  • Is the above statement legitimate?
  • Why is this the case? (Was it simply an oversight, or were the creators of the HTML spec trying to prevent specific problems by creating this rule?)
  • What is the recommended way to construct a form with inputs? (Are we just supposed to create a div or a table directly inside the form tag?)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's standards pedantics.

  • The statement is legitimate as far as the standard goes: in HTML 4.01, the definition for <form> specifies that it may only contain block elements or <script>. As far as what every browser in the world allows, it's fine.
  • I can only guess that they consider <form> to not be a layout tag at all, and they want all inline elements to be contained inside a block element.
  • Yes, you're supposed to place a <div>, <table>, <p>, or some other block presentational element inside the <form>.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...