不存在的属性HTML输入字段 [英] Non existing Attributes HTML Input Field
问题描述
将非现有属性附加到html元素以便在jquery中使用它是一种好习惯。例如
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" />
然后只需使用jquery选择包含名为valrule的属性的所有元素并解析正则表达式字符串。
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?
推荐答案
您应该使用前缀 data-< myAttribute>
。 HTML5支持它,其他浏览器会忽略它,您可以使用 .data()
方法使用jQuery轻松访问它。
You 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 ;-)
这篇关于不存在的属性HTML输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!