Why single textarea mess all following xhtml?

2019-07-20 20:38发布

问题:

I encounter a problem in my web program. I got a textarea in my form, sometimes there is nothing in textarea, so genshi template engine just output it as

<textarea xxxx />

and here comes the problem, all following tags are in the textarea. Why all browser can't handle single textarea correctly?

If I write it as

<textarea xxxx></textarea>

and everything works fine. Why a single textarea messes following tags in xhtml?

回答1:

Because you are, presumably, serving your XHTML with a text/html Content-type and causing it to be processed as HTML. XML style self-closing tags do not exist in HTML (so you have to use explicit start and end tags, except where they are forbidden (e.g. end tags on img) or optional (e.g. start and end tags on the body element).

If you want to have your pages processed as XHTML then serve as application/xhtml+xml (and kiss goodbye to support from Internet Explorer).

(This is why I stick to HTML 4.01 for most projects)

See http://www.w3.org/TR/xhtml-media-types/ for more details (including the full set of compatibility guidelines to munge your XHTML into a shape where HTML user agents can cope with it).