使用非标准HTML属性被认为是不好的做法? [英] Is it considered bad practice to use non-standard HTML attributes?

查看:87
本文介绍了使用非标准HTML属性被认为是不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < a stackoverflowId =123> ..我发现设置任意属性非常方便。 < / A> 

在JavaScript中:

  var soId = $ selectofA.attr('stackoverflowId'); // jQuery 

这是一个很好的练习吗?我从来没有遇到过这个问题。

在HTML 5中,您可以使用 data - * attribute:

 < a id =myDivdata-stackoverflowId = 123\" > ...< / A> 






由于您使用的是jQuery,因此您可能还想要检查 jQuery元数据插件 。它将允许您在类属性中存储和解析JSON元数据,并且您的标记仍然有效。



您可以像这样插入元数据:

 < a class =your_class {stackoverflowId:'123'}> ...< / a> 

解压缩如下:

  var data = $ selectofA.metadata(); 


I find it very convenient to set arbitary attributes like:

<a stackoverflowId="123">...</a>

And in JavaScript:

var soId = $selectofA.attr('stackoverflowId'); //jQuery

Is it a good practice at all? I've never had a problem with this yet.

解决方案

In HTML 5 you can do this with the data-* attribute:

<a id="myDiv" data-stackoverflowId="123">...</a>


Since you are using jQuery, you may also want to check the jQuery Metadata Plugin. It will allow you to store and parse JSON metadata in the class attribute, and your markup will still validate.

You would be able to insert your metadata like this:

<a class="your_class {stackoverflowId: '123'}">...</a>

And extract it as follows:

var data = $selectofA.metadata();

这篇关于使用非标准HTML属性被认为是不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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