jQuery - select all elements with attribute name (not value) beginning with...?(jQuery - 选择属性名称(不是值)以...开头的所有元素?)
问题描述
假设我正在寻找具有属性数据语言"的所有元素,其值以java"开头(将匹配java"和javascript").我知道该怎么做:
Say I'm looking for all elements with an attribute 'data-language', whose value begins with 'java' (would match both 'java' and 'javascript'). I know how to do this:
$('[data-language^="java"]')
但我的问题是:如何获取所有具有属性的元素(不是属性的值,而是实际的属性名称本身)?例如:
But my question is: how do I get all elements that have an attribute (not the value of the attribute, but the actual attribute name itself) beginning with something? For example:
- 属性名称以data-s"开头的所有元素,或
- 所有具有数据属性的元素(属性以data-"开头).
推荐答案
没有捷径,可以使用attributes
集合:
There is no shortcut, you may use the attributes
collection :
$(someselector).filter(function(){
var attrs = this.attributes;
for (var i=0; i<attrs.length; i++) {
if (attrs[i].name.indexOf("someStartOfName")==0) return true;
}
return false;
});
这篇关于jQuery - 选择属性名称(不是值)以...开头的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jQuery - 选择属性名称(不是值)以...开头的所有元素


- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 失败的 Canvas 360 jquery 插件 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 400或500级别的HTTP响应 2022-01-01