开发者

How bad is it to add custom attributes to HTML tags? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Custom attributes - Yay or nay?

Hello, it might be a very basic question but I wonder if are there any implications to set custom attributes to HTML tags... in terms browsers compatibility, SEO, complainants etc.

thanks 开发者_如何学C


If you're using HTML5, you are absolutely allowed to add custom attributes to your tags. Here's an article on the subject: http://ejohn.org/blog/html-5-data-attributes/

If you're stuck using an older HTML or xhtml version then by rights you shouldn't be adding attributes. However for the most part the browser will accept it. You should test comprehensively though, as it's outside the spec so different browsers may behave differently.

In xhtml, you definitely can't add attributes, since it would break the document definition and won't validate. However (as stated in the article I linked earlier), you can add them using a different namespace if you need to.


Well, your code won't validate but I doubt it will really cause much of a problem. What is the purpose of these custom attributes and could they be replicated with the HTML5 data attribute?

(No "HTML5 isn't ready" responses please)


Only problems would come with validation. You can add as many attributes as you want, but they won't pass validation tests.

As someone else said, they won't be recognized by default, you would probably have to write your own javascript or something to be able to read them.


In most cases where you are adding attributes to HTML that isn't standard markup or CSS you want to store data for JavaScript to have for interactions. Look into the jQuery.data() capabilities and that may give you what you're looking for.

http://api.jquery.com/jQuery.data/


In general, if you set your own attributes on HTML elements, you will have to build a renderer that understands those attributes (and make sure that your users have the renderer).

Usually, this means you provide javascript that understands your new attributes. The only disadvantage with this is that to serve your HTML you also need to serve your javascript; you can't decouple the two.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜