I've just notice a routine in our code that doesn't proper开发者_如何转开发ly escape an ampersand in a textarea:
<textarea>Arts & Crafts</textarea>
What surprises me is that this works in every browser I check. Is there an unwritten rule about ampersands inside textareas? If so, is it a rule I can depend on? Or is this just a convenience that browsers provide to make sure your faulty page renders and there are no guarantees whatsoever?
Most browsers will try to fix errors made in the page. The ampersand cannot mean anything else than an ampersand, so it is shown as such. Alternatives would be to skip the ampersand, or not to render the page at all, but all vendors have thought this fallback to be the most logical one.
It will give you problems when the text is for example <textarea>Use è to show an è</textarea>
, because this will render è
as an è
too. So in this case the ampersand must be escaped.
It is however one of the very few errors that are solved the same way in any browser. It is best not to make any error. That will give you the greatest chance on succes. ;)
It's just a convenience; a standalone &
is invalid HTML and XHTML.
精彩评论