简体   繁体   English

HTML标记上的非标准属性。好事?坏事?你的意见?

[英]Non-Standard Attributes on HTML Tags. Good Thing? Bad Thing? Your Thoughts?

HTML (or maybe just XHTML?) is relatively strict when it comes to non-standard attributes on tags. HTML(或者只是XHTML?)在标签上的非标准属性方面相对严格。 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. 然而,非标准属性对于将元数据传递给Javascript非常有用。 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: 或者,您可以将弹出窗口的标题存储在隐藏元素中,例如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). 我是提议的HTML 5解决方案( data-前缀属性)的忠实粉丝。 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). 例如,自定义应用程序将使用的标准属性中没有模拟的数据(例如,基于不一定用className或id表示的事物的事件处理程序的自定义)。

Custom attributes provide a convenient way to carry extra data to the client side. 自定义属性提供了将额外数据传输到客户端的便捷方式。 Dojo Toolkit is doing this regularly and it has been pointed ( Debunking Dojo Toolkit Myths ) out that: Dojo Toolkit定期执行此操作,并指出( Debunking Dojo Toolkit Myths ):

Custom attributes have always been valid HTML, they just don't validate when tested against a DTD. 自定义属性始终是有效的HTML,它们只是在针对DTD进行测试时不进行验证。 [...] The HTML specification states that any attribute not recognized is to be ignored by the HTML rendering engine in user agents, and Dojo optionally takes advantage of this to improve ease of development. [...] HTML规范规定,用户代理中的HTML呈现引擎会忽略任何未识别的属性,Dojo可以选择利用此功能来提高开发的便利性。

Another option would be to define something like this in Javascript: 另一个选择是在Javascript中定义这样的东西:

<script type="text/javascript">
var link_titles = {link1: "Title 1", link2: "Title 2"};
</script>

Then you can use this later in your Javascript code, assuming your link has an ID that corresponds to the ID in this hashtable. 然后,您可以稍后在Javascript代码中使用它,假设您的链接具有与此哈希表中的ID对应的ID。

It doesn't have the disadvantages of the other two methods: no non-standard attributes nor the ugly hidden span. 它没有其他两种方法的缺点:没有非标准属性,也没有丑陋的隐藏跨度。

The disadvantage is that it might a bit of an overkill for things as simple as your example. 缺点是对于像你的例子那样简单的事情可能有点过分。 But for more complex scenarios, where you have more data to pass, it's a good choice. 但对于更复杂的场景,您需要传递更多数据,这是一个不错的选择。 Especially considering that the data is being passed as JSON, so you can pass complex objects with ease. 特别是考虑到数据是作为JSON传递的,因此您可以轻松地传递复杂对象。

Also, you keep data separate from the formatting, which is a good thing for maintainability. 此外,您将数据与格式分开,这对可维护性是一件好事。

You can even have something like this (which you can't really do with the other methods): 你甚至可以拥有这样的东西(你不能用其他方法真正做到):

var poi_types = {1: "City", 2: "Restaurant"};
var poi = {1: {lat: X, lng: Y, name: "Beijing", type: 1}, 2: {lat: A, lng: B, name: "Hatsune", type: 2}};

... ...

<a id="poi-2" href="/poi/2/">Hatsune</a>

And since you most probably use some server-side programming language, this hash table should be trivial to generate dynamically (just serialize it to JSON and spit it in the header section of the page). 而且由于您最有可能使用某种服务器端编程语言,因此该哈希表应该是动态生成的(只需将其序列化为JSON并将其吐入页面的标题部分)。

Why not declaring the popup_title attribute in a custom DTD ? 为什么不在自定义DTD中声明popup_title属性? This solves the problem with validation. 这解决了验证问题。 I do this with every non-standard elements, attributes and values and thank this validation shows me only real problems with my code. 我使用每个非标准元素,属性和值执行此操作,并且感谢此验证向我显示了我的代码中的实际问题。 This makes also any browser errors less possible with such HTML. 这也使得使用此类HTML的浏览器错误变得更少。

Well in this case, the optimal solution is 那么在这种情况下,最佳解决方案是

<a href="#" alt="" title="Title of My Pop-up">click</a>

and using title attribute. 并使用title属性。

Sometimes I break the spec if I really need it. 如果我真的需要它,有时我会违反规范。 But rarely, and only for good reason. 但很少,只有充分的理由。

EDIT: Not sure why the -1, but I was pointing out that sometimes you think you need to break spec, when you don't. 编辑:不知道为什么-1,但我指出,有时你认为你需要打破规格,当你不这样做。

You could nest hidden input elements INSIDE the anchor element 您可以将隐藏的输入元素嵌入到锚元素中

<a id="anchor_id">
  <input type="hidden" class="articleid" value="5">
  Link text here
</a>

Then you can easily pull the data out by 然后,您可以轻松地将数据拉出来

$('#anchor_id .articleid').val()

My solution in the end was to hide additional data in the id tag separated by some sort of delimiter (one underscore is a space, two is the end of that arg), the second arg there is an id: 我的解决方案到底是隐藏了由某种分隔符分隔的id标签中的其他数据(一个下划线是一个空格,两个是该arg的结尾),第二个arg有一个id:

<a href="#" class="article" id="Title_of_My_Pop-up__47">click</a>

Ugly, and it assumes you're not already using the id tag for something else, but it is compliant across every browser. 很丑,它假设你还没有将id标签用于其他东西,但它在所有浏览器中都是兼容的。

I've been racking my brain over this as well. 我也一直绞尽脑汁。 I like the readability of non-standard attributes, but I don't like that it will break standard. 我喜欢非标准属性的可读性,但我不喜欢它会破坏标准。 The hidden span example is compliant, but it is not very readable. 隐藏的span示例是兼容的,但它不是非常易读。 What about this: 那这个呢:

<a href="#" alt="" title="" rel="{popup_title:'Title of My Pop-up'}">click</a>

Here the code is very readable, because of JSON's key/value pair notation. 这里的代码非常易读,因为JSON的键/值对表示法。 You can tell that this is meta data that belongs link just by looking at it. 您可以通过查看它来判断这是属于链接的元数据。 The only flaw I can see beside hijacking the "rel" attribute is that this would get messy for complex objects. 我在劫持“rel”属性旁边看到的唯一缺陷是,这对于复杂的对象来说会变得混乱。 I really like that idea of "data-" prefixed attributes mentioned above. 我真的很喜欢上面提到的“数据 - ”前缀属性的想法。 Do any current browsers support this? 当前的浏览器是否支持此功能?

Here is something else to think about. 这是另外需要考虑的事情。 How much impact does not compliant code have on SEO? 兼容代码对SEO有多大影响?

My personal feeling in your example is that the span route is more appropriate, as it meets the standards of the XHTML specification. 在您的示例中,我个人的感觉是跨度路径更合适,因为它符合XHTML规范的标准。 However, i can see an argment for custom attributes, but I think they add a level of confusion that isn't needed. 但是,我可以看到自定义属性的细分,但我认为它们会增加一些不需要的混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM