admin管理员组文章数量:1315252
Is it good practice to attach an non existing attribute to an html element in order to use it in jquery. For example
<input type="text" valrule="someregexstring" />
then simply use jquery to select all elements which contain an attribute called valrule and parse the regex string.
Is this a 'no-go' as far as programming standards and best practices go?
Is it good practice to attach an non existing attribute to an html element in order to use it in jquery. For example
<input type="text" valrule="someregexstring" />
then simply use jquery to select all elements which contain an attribute called valrule and parse the regex string.
Is this a 'no-go' as far as programming standards and best practices go?
Share Improve this question edited Mar 13, 2012 at 12:56 Marek Sebera 40.7k37 gold badges164 silver badges249 bronze badges asked Mar 13, 2012 at 12:55 Friso KluitenbergFriso Kluitenberg 1,1792 gold badges15 silver badges36 bronze badges4 Answers
Reset to default 7You should use the prefix data-<myAttribute>
. It is supported by HTML5, other browsers will ignore it and you can access it easily with jQuery using the .data()
method.
<input id="myInput" type="text" data-MySuperMassiveAttribute="Awesome"/>
and you can retrieve that information like so :
var howAmI = $('#myInput').data('MySuperMassiveAttribute');
alert(howAmI); // now everybody knows how awesome you are ;-)
Use HTML5 data- prefix for new attribute like this:
<input type="text" data-valrule="someregexstring" />
Its valid HTML 5..
http://ejohn/blog/html-5-data-attributes/
You definitely can. Although HTML5 introduced what's called data- attributes, allowing to set non-standard attributes to elements in a standard way.
You simply prefix the attribute names with data-
<input type="text" data-valrule="someregexstring" />
Although, this is introduced in HTML5, you can still use it with documents for HTML4.
Using jQuery (1.4.3+), the data- attributes can be accessed with .data() - or via .attr().
I personally wouldn't do that. Some browsers ignore "non-existing" attributes when parsing HTML, therefore these attributes wouldn't be saved in the browser page DOM. When jQuery selectors are executed, they may not find this attribute.
If you are guaranteed to run in HTML5-pliant browsers, then you can use HTML5 data-
prefix for your attributes, e.g.
<input type="text" data-valrule="someregexstring" />
However this still may not work if you run in older browsers. For example, it turns out that almost half of our clients are using IE7 with no plans to upgrade (moving with the speed of government).
本文标签: javascriptNon existing Attributes HTML Input FieldStack Overflow
版权声明:本文标题:javascript - Non existing Attributes HTML Input Field - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741980636a2408376.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论