HTML标签上的非标准属性。好东西?坏事?你的想法? [英] Non-Standard Attributes on HTML Tags. Good Thing? Bad Thing? Your Thoughts?

查看:143
本文介绍了HTML标签上的非标准属性。好东西?坏事?你的想法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于标签上的非标准属性,HTML(或者也许只是XHTML?)相对比较严格。如果它们不是规范的一部分,那么你的代码被认为是不合规的。



非标准属性对于将元数据传递给Javascript非常有用然而。例如,如果链接假设显示一个弹出窗口,您可以在一个属性中设置弹出窗口的名称:

 < ; a href =#nullclass =popuptitle =查看弹出窗口! 
popup_title =标题为我的弹出窗口>点击我< / a>

或者,您可以将弹出窗口的标题存储在隐藏元素中, p>

 < style> 
.popup .title {display:none; }
< / style>
< a href =#nulltitle =查看弹出窗口!类= 弹出 >
点击我
< span class =title>标题为My Popup< / span>
< / a>

然而,我很担心哪些应该是首选的方法。第一种方法更简洁,而且我猜测,它不会像搜索引擎和屏幕阅读器那么多。相反,第二种选择使得存储大量数据变得更容易,因此更通用。它也符合标准。



我很好奇这个社区的想法是什么。你如何处理这样的情况?第一种方法的简单性是否超过了潜在的不利因素(如果有的话)? 解决方案

我是提出了HTML 5解决方案( data - 前缀属性)。编辑:我会补充说,使用自定义属性可能有更好的例子。例如,自定义应用程序将使用的数据在标准属性中没有模拟(例如,基于不能用className或id表示的事件的事件处理程序的自定义)。


HTML (or maybe just XHTML?) is relatively strict when it comes to non-standard attributes on tags. If they aren't part of the spec, then your code is considered non-compliant.

Non-standard attributes can be fairly useful for passing along meta-data to Javascript however. For instance, if a link is suppose to show a popup, you can set the name of the popup in an attribute:

<a href="#null" class="popup" title="See the Popup!" 
   popup_title="Title for My Popup">click me</a>

Alternatively, you can store the title for the popup in a hidden element, like a span:

<style>
    .popup .title { display: none; }
</style>
<a href="#null" title="See the Popup!" class="popup">
    click me
    <span class="title">Title for My Popup</span>
</a>

I am torn however as to which should be a preferred method. The first method is more concise and, I'm guessing, doesn't screw with search engines and screen readers as much. Conversely, the second option makes storing large amounts of data easier and is thus, more versatile. It is also standards compliant.

I am curious what this communities thoughts are. How do you handle a situation like this? Does the simplicity of the first method outweigh the potential downsides (if there are any)?

解决方案

I am a big fan of the proposed HTML 5 solution (data- prefixed attributes). Edit: I'd add that there are probably better examples for the use of custom attributes. For instance, data that a custom application will use that have no analogue in standard attributes (eg. customization for event handlers based on something that can't necessarily be expressed in a className or id).

这篇关于HTML标签上的非标准属性。好东西?坏事?你的想法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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