What's stopping me from using arbitrary tags in HTML?
Even the new HTML5 tags aren't enough to describe structures without falling back to div
s. What's stopping me from changing:
<div class="post">
<div class="userinfo">
<span class="name">Casey</span>
<img class="avatar" src="..." />
</div>
<div class="body">
<p>blah blah blah</p>
<p>blah blah blah</p>
<p>blah blah blah</p>
</div>
</div>
into something like:
<post>
<userinfo>
<name>Casey</name>
<img class="avatar" src="..." />
</userinfo>
<pbody>
<p>blah blah blah</p>
<p>blah开发者_如何学Go blah blah</p>
<p>blah blah blah</p>
</pbody>
</post>
To me, the second example is a lot cleaner. Is there anything (i.e., browser support) stopping me from doing this?
(I realize what it is is, essentially, XML, but in that case, the question becomes, "what does browser support look like for rendering XML web pages?")
One reason is that Internet Explorer (and earlier versions of Firefox) don't have a fallback for tags that are not defined and wind up ignoring them for both styling and nesting. Without a shiv, your site will break horribly in those browsers.
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.
Good news
- Nothing is stopping you
Use as many custom tags as you like, browsers will know what you mean. Not just the modern ones but IE7+ too. - No default look
Browsers will treat your tag as unknown and apply no default style to it. No weird margins, paddings, no surprises across clients and platforms. Thedisplay
property is not implied either so give it a value, but after that, everything is as usual. DOM contains your node,querySelector
works, styles apply, etc. - Super readable
You'll love the new look of your HTML source.
Things to consider
Self-closing or not - to make your tag self-closing, use the good old
<something/>
format, as you would with<br/>
for example. It's important, otherwise browsers go look for the closing tag. I mean, how could they not.Future collisions - that's the only good point from custom tag skeptics: just as we have a lot of new tags in HTML5, it's a possibility that you name a tag "icon" and it will mean something in the next HTML standard, even with a bunch of rendering defaults, and that can mess your page up badly. So I'd say, if you want to be on the safe side, use
<dashed-names>
for your tags, they will never ever mess with dashed tag names thanks to the naming of Web Components. Also check out § 4.13 in HTML standard itself.Blame magnet effect - seriously, that's an issue with custom tags, I've been down this road. Use custom tags only if everyone working on the same project is on board with it. Otherwise, whenever something breaks it will be your fault "because of your stupid custom tags", and you'll have to change every instance to the usual
<div class="gurgleburgle">
, only to find later that the real issue was totally unrelated.
TLDR:
<custom-tags> Yes you can use them </custom-tags>
Most browsers will just treat the tags as arbitrary (like how old browsers treat HTML5 tags). Nothing is stopping you from using your own tags, but it's not a well-accepted way to code HTML. HTML is supposed to use pre-defined tags.
If you're interested in coding with arbitrary tags, you could just go with XML. You can format XML with XSLT (used in a way similar to stylesheets, but much more powerful). Have a look here: http://www.w3schools.com/xml/xml_xsl.asp
What is the goal of the tags you chose? If your goal is to present information, then using divs and other presentation-oriented structures is great. Your tags look more like they are attempting to describe the actual data. If that is the case, then XML with XSLT transforms on the server side to output HTML for presentation markup is best. Remember that a browser is simply a rendering engine and it uses the HTML spec as it's blueprint of what to render for a given site. The browser doesn't need to understand the information like a "post" or "userInfo" because it has no context for undertsanding what to do from a rendering perspective with that information. CSS can help a browser understand what you want to do visually but ask yourself first whats the goal of having your markup that way, to store your data (XML-style) or to present it. If to present it, then you should go with the standards if you want to continue to use a browser as your rendering engine. Good luck, would be great if we could all define our presentation schemes though, fun idea!
getElementsByTagName fails me with custom tags. Example,
<acme:mytag id="mytag">
<div id ="x">x</div>
<div id ="y">y</div>
<div id ="z">z</div>
</acme:mytag>
This fails with IE8 (Quirks Mode or Standard Mode)
var mytag = document.getElementById('mytag'); // it's found
var mydivs = mytag.getElementsByTagName ('div'); // but this is always 0
Unless your html tag reads
<html XMLNS:acme>
...
</html>
The issue is that it would not validate and the new tags would simply be ignored.
精彩评论