HTML illegal tags does not error out

穿精又带淫゛_ 提交于 2021-02-11 12:51:46

问题


In html how is it that illegal tags work? Please see the example below -

<html>
<body>
<sdbfksbdfksbadfkb>
This is the content!!!
<gft34534534>
</body>

This is the content is displayed even though its inside illegal tags.


回答1:


Browsers are designed to be forwards compatible with new versions of HTML.

Unrecognised elements get added to the DOM (defaulting to having minimal styling and displaying as inline elements).

That way, using new markup from new versions of HTML won't hide content in old browsers.




回答2:


Modern browsers render unknown tags kind of like <div> elements, except rendered with display: inline instead of display: block. That way it's possible to introduce new elements like the ones which came with HTML5 without crashing in older browsers.

Older versions (especially IE) could have problems with it. But even those will display it in some way.




回答3:


Browsers generally do not throw error messages when they counter invalid markup. This is understandable since most web pages are invalid one way or another, so a browser that signalled errors to users (who cannot fix them) would not become too popular. Browsers may signal some markup errors in the console log (which most users don’t look at), though it seems that mostly just modern versions of IE do that.

HTML5 specifies how unknown elements are to be handled. This is in principle just mandatory error processing, not an extension mechanism. An unknown element is parsed and inserted into the DOM tree as a node that has just the general properties of an element, with no default rendering and no default action. You can still use scripting on it and set CSS styles on it, except that in old versions of IE, styling is not possible unless you first “introduce” the element in a script, with an invocation like document.createElement('sdbfksbdfksbadfkb)`.

In the example case, browsers will imply the missing closing tags at the end of the document. However, if the document is being processed as genuine XHTML (it has been served with an XML content type), then the example causes an error message to be shown, without showing the document at all. But this is because of violating well-formedness constraints, due to lack of end tags, not because of unknown element names.



来源:https://stackoverflow.com/questions/26213028/html-illegal-tags-does-not-error-out

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