开发者

JQuery : Is it bad practice to use a custom attribute in my html code?

I've been adding custom attributes which I reference using JQuery,开发者_如何学编程 this works great. But is it good practice?

example:

<div class="monkeys" customattr="big Monkey"> </div>

thanks all


Best practice is to use HTML5 data-* attributes:

<div class="monkeys" data-customattr="big Monkey"> </div>

This is standards-compliant HTML5, unlike arbitrary custom attributes. It also makes sure that your custom attribute won't conflict with some future standard attribute.

In recent (1.5+) versions of jQuery, you can also use $('.monkeys').data('customatrr') to access the attribute.


Yes, it is. If you're not using HTML 5 (see @SLaks post) why not change your class to be descriptive?

<div class="big monkeys"> </div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜