Original connection: http://www.dudo.org/article.asp?id=253
There is a standard in the XHTML specification that each XHTML tag has an end tag. So for elements in HTML that originally did not have the end tag, add / before the end to close the tag, for example, the original writing method of the <img> tag in HTML:
<img src=... alt=...>
In XHTML, you should add a / before > to close this tag. In order to prevent some old browsers from not recognizing this writing method, you should add a space before / (HTML compatibility standard):
<img src=... alt=... />
This seems to mean that adding a / to the start tag can close the tag. Especially for empty tags, it seems that using this writing method is more concise. For example,
<div class=clear></div>
This is a piece of code that is often used in clearing floating elements (closing floating elements). Then it seems to be possible to write:
<div class=clear />
Yes, the <div> element can be empty, so there is no problem writing this way, but what will happen if we use similar writing methods for other tags? If you introduce external Javascript files in <head>, we usually write this:
<script type=text/javascript src=... language=javascript></script>
Since there is no content, can we write it as
<script type=text/javascript src=... language=javascript />
We found that this writing method either does not work or will make an error in the browser. In most cases, the browser will think that this tag is not closed, and mistakenly recognizes that all the content after <script> is Javascript code, so an error will occur. For example,
<textarea id=tt cols=10 rows=8 />
If you run this code in the browser, you will find that starting from <textarea>, all XHTML codes afterwards will appear in the text box as the content of the text box.
It seems that pairs of tags already exist in HTML4.0 must use the existing end tag in XHTML. So why is <div>ok? In fact, <div> is not allowed, it is just that <div> is not so obvious in the browser. And writing methods such as <span /> are even more incorrect. First of all, <span> has a closed tag</span> itself, and in addition, the content of the <span> tag cannot be blank (normal spaces). In other words, except for the tags such as <img>, <hr>, <link>, <br> that can be used/closed, other elements must be used</...>, otherwise unpredictable problems will occur.