具有以字符串开头的属性的元素的jQuery选择器 [英] jQuery selector for elements with attribute starting with string

查看:133
本文介绍了具有以字符串开头的属性的元素的jQuery选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要选择所有具有以给定前缀开头的属性的元素-请注意,我所说的是属性名称, not 值.例如:

I need to select all elements, which have an attribute starting with a given prefix - note I am talking about the attribute name, not value. For example:

<div data-abc-name="value">...</div>
<a href="..." data-abc-another="something">...</a>
<span data-nonabc="123">...</span>

在上面的HTML中,我需要获取所有具有以data-abc-开头的属性的元素-即diva.

In the above HTML, I need to get all elements that have an attribute starting with data-abc- - that is, the div and the a.

我该怎么做?

推荐答案

您可以在ES6中这样做:

You can do it like this by ES6:

$('*').filter(function() {
  for (attr of this.attributes)
    if (attr.name.startsWith("data-abc"))
      return this;
});

在线演示(jsFiddle)

这篇关于具有以字符串开头的属性的元素的jQuery选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆