Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

javascript - How does AngularJS get away with using custom HTML5 element tags and attributes?

Most browsers seem to allow you to create any element tag you like, and add any attribute name you like to elements. For example:

<!DOCTYPE html>
<html>
<body>
<div my-attribute="has no data- prefix, but seems to behave like an attribute should">
    <br/>
    <hello-world style="display:block;background:#eee">Hello Everybody</hello-world>
    <goodby-world style="background:#faa">Default display is inline</goodbye-world>
</div>
</body>
</html>

The above displays fine. It looks like undefined elements behave pretty much like spans. It also looks like AngularJS takes advantage of custom tags & attributes quite a lot - it pretty much depends on it.

But, as far as I understood, doing that sort of thing was a no-no, except in the case of custom html5 attributes with the "data-" prefix.

So, my question is: Is it no longer taboo to make up your own custom element tags and/or attribute names? Or put another way, does AngularJS rely on non-standard quirks that are not really part of the HTML5 spec, but just happen to work anyway? Did somebody at google discover a long-lost secret chapter of the html5 spec? Or am I totally misunderstanding something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It won't validate but it will render.

HTML5 is designed to be very forgiving (contrary to XHTML). This is in part so that "older" HTML5 browsers can allow new elements (as well as badly formatted HTML) which that version doesn't support yet without breaking the page.

This can of course be "exploited" to introduce custom tags.

That being said - you can also do this with HTML4.

As mentioned in comments: you also have the ability to prefix any attributes in an ordinary tag with data- and it will validate.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...