html5数据属性是否区分大小写? [英] are html5 data attributes case insensitive?

查看:1591
本文介绍了html5数据属性是否区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近注意到Chrome将数据属性数据 - * 转换为小写。

I recently noticed that chrome converts data attributes data-* to lower case.

例如

< div data-Me =awesome>< / div> 将在Chrome开发工具中转换为< div data-me =awesome>< / div>

<div data-Me="awesome"></div> will be converted to <div data-me="awesome"></div> in Chrome Dev Tools.

当我用javascript调用该属性时:

When I call the attribute with javascript:

console.log(e.hasAttribute('data-Me'),e.hasAttribute ('data-me'));

返回 true

那么命名数据属性的标准方法是什么?

So whats the standard way to name data attributes?

推荐答案

您应该始终使用小写字符。即使某些浏览器会自动纠正标记中的错误(这就是Chrome在这里为您所做的),但它可能会导致错误并且无效HTML5。
来自 MDN

You should always use lowercase characters. Even though some browser do automatically correct mistakes in the markup (that's what Chrome does for you here) it can cause errors and isn't valid HTML5. From MDN:


  • 该名称不得以xml开头,无论用于这些
    字母的情况如何;

  • 该名称不得包含任何分号(U + 003A);

  • 名称
    不得包含大写字母A至Z字母。

EDIT

稍微深入一点我发现 this


HTML文档中HTML元素的所有属性名称都会自动获得
ASCII-lowercased,因此对ASCII大写
字母的限制不会影响此类文档。

All attribute names on HTML elements in HTML documents get ASCII-lowercased automatically, so the restriction on ASCII uppercase letters doesn't affect such documents.

无论如何,我建议你从一开始就以正确的方式进行。

Regardless of that fact, I recommend you to do it the right way directly from beginning.

这篇关于html5数据属性是否区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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