我有一个可能类似于 abcjQuery12345
的属性,这些数字只是一个示例。
有没有办法确定 jQuery 中包含子字符串的属性?
例如abcjQuery12344353452343452345="12"
我唯一知道的是该属性将包含 abc
。
最佳答案
您可以循环遍历所有属性并检查它们是否包含您要查找的字符串。与:
<div customAttr="12" title="me"></div>
<div anotherAttr="Bear"></div>
<div yetAnotherAttr="Elephant"></div>
您可以使用:
var m = /another/i; //will look for 'another' in the attribute name (i-flag for case-insensitive search)
$('div').each(function(){
var $this = $(this);
$.each(this.attributes,function(){
if (this.name.match(m)){
$this.addClass('selected'); //either select the matching element
$this.text(this.name + ' : ' + this.value); //or use its custom-attribute's value
}
});
});
参见a fiddle
关于javascript - 如何使用 jQuery 获取自定义属性的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10534002/