javascript - 如何使用 jQuery 获取自定义属性的名称?

标签 javascript jquery

我有一个可能类似于 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/

相关文章:

javascript - CKEDITOR - 无法编辑文本或使用任何按钮

java - 是否有任何 API 或工具可用于使网页表高度可配置?

jQuery AJAX 和 JSON 格式

javascript - 下划线/lodash 模板上的事件绑定(bind)

php - 如何锁定其他网页

javascript - 淡入淡出的一些问题(jQuery)

javascript - ExtJS 4 中字段旁边的标签

javascript - 使用 Typescript 使用默认导出

jquery CSS calc() 函数

javascript - 不要在浏览器或选项卡关闭时销毁 Vue-Cookies