AngularJS 如何避免使用自定义 HTML5 元素标签和属性? [英] How does AngularJS get away with using custom HTML5 element tags and attributes?

查看:26
本文介绍了AngularJS 如何避免使用自定义 HTML5 元素标签和属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数浏览器似乎允许您创建任何您喜欢的元素标签,并为元素添加您喜欢的任何属性名称.例如:

<身体><div my-attribute="没有数据前缀,但似乎表现得像一个属性应该"><br/><hello-world style="display:block;background:#eee">大家好</hello-world><goodby-world style="background:#faa">默认显示为内联</goodbye-world>

</html>

以上显示正常.看起来 undefined 元素的行为与 span 非常相似.看起来 AngularJS 也利用了自定义标签 &属性很多 - 这在很大程度上取决于它.

但是,据我所知,做那种事情是不允许的,除了带有data-"前缀的自定义 html5 属性.

所以,我的问题是:组成自己的自定义元素标签和/或属性名称不再是禁忌吗?或者换句话说,AngularJS 是否依赖于非标准的怪癖,这些怪癖不是 HTML5 规范的真正组成部分,但无论如何都可以正常工作?谷歌有人发现了 html5 规范中久违的秘密章节吗?还是我完全误解了什么?

解决方案

它不会验证但会呈现.

HTML5 的设计非常宽容(与 XHTML 不同).这在一定程度上是为了旧的"HTML5 浏览器可以允许该版本尚不支持的新元素(以及格式错误的 HTML)而不会破坏页面.

这当然可以被利用"来引入自定义标签.

话虽如此 - 您也可以使用 HTML4 做到这一点.

正如评论中提到的:您还可以在普通标签中使用 data- 为任何属性添加前缀,它会进行验证.

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?

解决方案

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.

这篇关于AngularJS 如何避免使用自定义 HTML5 元素标签和属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆