HTML5数据属性中的通配符 [英] Wildcards in HTML5 data attributes
问题描述
请考虑以下HTML:
$ b 可以在属性名称中找到所有带有通配符的jQuery元素? $ b
< input
id =val1
type =text
数据验证所需
data- validate-minlength =3
data-validate-email />
我试图实现的是找到所有以<$ c开头的属性名的节点$ c> data-validate -
就我所了解的通配符而言这里与属性的值有关。
原因是 - I想要找出哪些元素应该被验证 - 然后找出哪些验证参数(如-email)进来。
谢谢
您可以创建一个自定义伪类为例如将属性名称与正则表达式匹配: http://jsfiddle.net/hN6vx/ 。
jQuery.expr.pseudos.attr = $ .expr.createPseudo(function(arg){
var regexp = new RegExp(arg);
return函数(elem){
for(var i = 0; i< elem.attributes.length; i ++){
var attr = elem.attributes [i];
if(regexp。 test(attr.name)){
return true;
}
}
return false;
};
});
用法:
$(:attr('^ data-'))
Is it possible to find all DOM elements with jQuery with wildcards in the attribute name?
Consider the following HTML:
<input
id="val1"
type="text"
data-validate-required
data-validate-minlength="3"
data-validate-email />
What I am trying to achieve is to find all dom nodes with an attribute name starting with data-validate-
As far as I understand the wildcards described here are concerned with "value" of the attribute.
The reason for this is - I want to find out which elements should be validated at all - and afterwards find out which validation parameters (like -email) comes in play.
Thanks
You can create a custom pseudoclass to e.g. match attribute names against a regexp: http://jsfiddle.net/hN6vx/.
jQuery.expr.pseudos.attr = $.expr.createPseudo(function(arg) {
var regexp = new RegExp(arg);
return function(elem) {
for(var i = 0; i < elem.attributes.length; i++) {
var attr = elem.attributes[i];
if(regexp.test(attr.name)) {
return true;
}
}
return false;
};
});
Usage:
$(":attr('^data-')")
这篇关于HTML5数据属性中的通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!