如何查找具有包含特定值的数据属性的HTML元素? [英] How to find HTML element with a data attribute which contains a specific value?
问题描述
我正在使用HTML中的数据属性,现在我想获取它.
I am using data attributes in HTML and now I want to get it.
我具有这样的属性:
<div class="row hidden" data-party-registration-source-type-id="3,4">
假设我的值为"3".我要搜索包含"3"的数据属性;
Suppose I have a value "3". I want to search for the data attribute that contains "3";
我该怎么做?
推荐答案
您可以使用jQuery属性包含选择器轻松实现此目标:
You can easily achieve this with jQuery attribute contains selector that:
选择具有指定属性并带有值的元素 包含给定的子字符串.
Selects elements that have the specified attribute with a value containing a given substring.
https://api.jquery.com/attribute-contains-selector/
这是最匹配的jQuery属性选择器 反对一个价值.如果选择器的字符串将选择一个元素 出现在元素属性值内的任何地方.
This is the most generous of the jQuery attribute selectors that match against a value. It will select an element if the selector's string appears anywhere within the element's attribute value.
示例:
$( "div.row[data-party-registration-source-type-id*='3']" )
这篇关于如何查找具有包含特定值的数据属性的HTML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!