自定义属性 - 是或不是? [英] Custom attributes - Yea or nay?

查看:99
本文介绍了自定义属性 - 是或不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直在阅读越来越多关于在HTML标签中使用自定义属性的人,主要是为了嵌入一些额外的数据以用于JavaScript代码。



我希望收集一些关于是否使用自定义属性的良好做法的反馈,以及一些替代方法。

看起来它确实可以简化 服务器端和客户端代码,但它也不符合W3C标准。

我们是否应该在我们的网络应用中使用自定义的HTML属性?为什么或为什么不?



对于那些认为自定义属性是好东西的人来说:使用它们时需要记住的一些事情是什么?



对于那些认为自定义属性不好的用户:您有什么替代方案可以完成类似的功能?

更新:我主要对各种方法背后的推理感兴趣,以及为什么一种方法比另一种更好。我想我们都可以拿出4-5种不同的方式来完成同样的事情。 (隐藏元素,内联脚本,额外的类,来自ID的解析信息等)。

更新2:看起来HTML 5 data - 属性功能有一个这里很多支持(我倾向于同意,它看起来像一个坚实的选择)。到目前为止,我还没有看到这个建议的反驳方式。是否有任何问题/陷阱担心使用这种方法?或者它仅仅是当前W3C规范的无害无效?HTML5显式地允许以<1>开头的自定义属性 code>数据。因此,例如,< p data-date-changed =Jan 24 5:23 p.m。>你好< / p> 是有效的。由于它是由一个标准正式支持的,我认为这是自定义属性的最佳选择。而且它并不要求你用黑客来重载其他属性,所以你的HTML可以保持语义。



来源: http://www.w3.org/TR/html5 /dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes


Recently I have been reading more and more about people using custom attributes in their HTML tags, mainly for the purpose of embedding some extra bits of data for use in javascript code.

I was hoping to gather some feedback on whether or not using custom attributes is a good practice, and also what some alternatives are.

It seems like it can really simplify both server side and client side code, but it also isn't W3C compliant.

Should we be making use of custom HTML attributes in our web apps? Why or why not?

For those who think custom attributes are a good thing: what are some things to keep in mind when using them?

For those who think custom attributes are bad thing: what alternatives do you use to accomplish something similar?

Update: I'm mostly interested in the reasoning behind the various methods, as well as points as to why one method is better than another. I think we can all come up with 4-5 different ways to accomplish the same thing. (hidden elements, inline scripts, extra classes, parsing info from ids, etc).

Update 2: It seems that the HTML 5 data- attribute feature has a lot of support here (and I tend to agree, it looks like a solid option). So far I haven't seen much in the way of rebuttals for this suggestion. Are there any issues/pitfalls to worry about using this approach? Or is it simply a 'harmless' invalidation of the current W3C specs?

解决方案

HTML 5 explicitly allows custom attributes that begin with data. So, for example, <p data-date-changed="Jan 24 5:23 p.m.">Hello</p> is valid. Since it's officially supported by a standard, I think this is the best option for custom attributes. And it doesn't require you to overload other attributes with hacks, so your HTML can stay semantic.

Source: http://www.w3.org/TR/html5/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes

这篇关于自定义属性 - 是或不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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