admin管理员组文章数量:1023081
Note: this isn't a basic question on Querying by data-attributes
The problem I'm having is that I have certain elements like so:
<div data-step="1, 4">
I have figured out how to step through each [data-step]
and extract the 1, 4
and create events for them, etc.
Let's say I'm trying to Query and grab this exact data-step but all I'm given is: 4
$('[data-step="4"]')
// this won't work of course
$('[data-step="1, 4"]')
// obviously this will work, but at this point I'm only given the index
// which will only be ONE of these numbers
Basically given (for examples sake, 4) How can I easily Query the selector to go out and grab [data-step="1, 4"]
?
All I'm able to e up with is a loop, that goes through each data-step, strips out everything and sees if there is a match. Is there an easier way potentially?
Note: this isn't a basic question on Querying by data-attributes
The problem I'm having is that I have certain elements like so:
<div data-step="1, 4">
I have figured out how to step through each [data-step]
and extract the 1, 4
and create events for them, etc.
Let's say I'm trying to Query and grab this exact data-step but all I'm given is: 4
$('[data-step="4"]')
// this won't work of course
$('[data-step="1, 4"]')
// obviously this will work, but at this point I'm only given the index
// which will only be ONE of these numbers
Basically given (for examples sake, 4) How can I easily Query the selector to go out and grab [data-step="1, 4"]
?
All I'm able to e up with is a loop, that goes through each data-step, strips out everything and sees if there is a match. Is there an easier way potentially?
Share Improve this question asked Jan 3, 2013 at 22:36 Mark Pieszak - Trilon.ioMark Pieszak - Trilon.io 67.4k15 gold badges83 silver badges96 bronze badges 2- 4 If you can change your data-step attribute to be delimited by spaces, you can use $("[data-step~='4']"). – ajshort Commented Jan 3, 2013 at 22:41
- @ajshort Seems like the simplest solution here! Luckily I'm able to change that, much easier to handle this way. Cheers – Mark Pieszak - Trilon.io Commented Jan 3, 2013 at 22:52
3 Answers
Reset to default 4~=
will find element with an attribute which has a value containing a given word, delimited by spaces. Works in your case also.
$('[data-step~="4"]')
*=
will find element with an attribute which has a value containing the given substring.
$('[data-step*="4"]')
You may also use filter
solution:
var search = 4;
$("[data-step]").filter(function() {
return $.inArray("" + search, $(this).data("step").split(/,\s*/)) !== -1;
});
You need to use * before = it will return all data-step wich is contain substring 'value' $('[data-step*="value"]')
Note: this isn't a basic question on Querying by data-attributes
The problem I'm having is that I have certain elements like so:
<div data-step="1, 4">
I have figured out how to step through each [data-step]
and extract the 1, 4
and create events for them, etc.
Let's say I'm trying to Query and grab this exact data-step but all I'm given is: 4
$('[data-step="4"]')
// this won't work of course
$('[data-step="1, 4"]')
// obviously this will work, but at this point I'm only given the index
// which will only be ONE of these numbers
Basically given (for examples sake, 4) How can I easily Query the selector to go out and grab [data-step="1, 4"]
?
All I'm able to e up with is a loop, that goes through each data-step, strips out everything and sees if there is a match. Is there an easier way potentially?
Note: this isn't a basic question on Querying by data-attributes
The problem I'm having is that I have certain elements like so:
<div data-step="1, 4">
I have figured out how to step through each [data-step]
and extract the 1, 4
and create events for them, etc.
Let's say I'm trying to Query and grab this exact data-step but all I'm given is: 4
$('[data-step="4"]')
// this won't work of course
$('[data-step="1, 4"]')
// obviously this will work, but at this point I'm only given the index
// which will only be ONE of these numbers
Basically given (for examples sake, 4) How can I easily Query the selector to go out and grab [data-step="1, 4"]
?
All I'm able to e up with is a loop, that goes through each data-step, strips out everything and sees if there is a match. Is there an easier way potentially?
Share Improve this question asked Jan 3, 2013 at 22:36 Mark Pieszak - Trilon.ioMark Pieszak - Trilon.io 67.4k15 gold badges83 silver badges96 bronze badges 2- 4 If you can change your data-step attribute to be delimited by spaces, you can use $("[data-step~='4']"). – ajshort Commented Jan 3, 2013 at 22:41
- @ajshort Seems like the simplest solution here! Luckily I'm able to change that, much easier to handle this way. Cheers – Mark Pieszak - Trilon.io Commented Jan 3, 2013 at 22:52
3 Answers
Reset to default 4~=
will find element with an attribute which has a value containing a given word, delimited by spaces. Works in your case also.
$('[data-step~="4"]')
*=
will find element with an attribute which has a value containing the given substring.
$('[data-step*="4"]')
You may also use filter
solution:
var search = 4;
$("[data-step]").filter(function() {
return $.inArray("" + search, $(this).data("step").split(/,\s*/)) !== -1;
});
You need to use * before = it will return all data-step wich is contain substring 'value' $('[data-step*="value"]')
版权声明:本文标题:javascript - jQuery Selecting DOM Element by data-attribute with multiple Identifiers in it - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745577273a2157097.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论