What's stopping me from using arbitrary tags in HTML?

前端 未结 7 868
礼貌的吻别
礼貌的吻别 2021-01-11 15:36

Even the new HTML5 tags aren\'t enough to describe structures without falling back to divs. What\'s stopping me from changing:

相关标签:
7条回答
  • 2021-01-11 16:31

    You can use your own tags, but the problem is that since they're not standard, browsers won't know that they may have matching closing tags. And of course, the document won't validate as proper HTML/X-HTML.

    <blah>
        This is some <span>test</span> test text with another <bogus>tag</bogus> tag
        within, which ends with a fake self-closing <tag />
    </blah>
    

    Browsers will see <blah>, not now how to deal with it, and treat it as essentially "nothing" and ignore it. Then they'll happily parse away on to the next bit, and see some plain text, with a valid span inside. Eventually they'll reach the </blah> and ignore that as well.

    This is why Javascript and CSS had to support the opening HTML comment sequence as part of their respective language definitions:

    <script type="text/javascript">
    <!--  // <--actually a part of the javascript spec, treated as a comment.
         alert('hey!');
    //-->
    </script>
    

    When Javascript was first introduced, there were still MANY other browsers out there that were entirely unaware of Javascript, and so they'd ignore tags, and happily output your Javascript code. Ditto for CSS.

    If you really need custom tags, you can produce the document as XML with your own DTD attached, and use XSLT to transform it into a valid HTML/X-HTML document.

    0 讨论(0)
提交回复
热议问题