如何在 jQuery 中选择 data-id 和 data-action
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7278854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to select data-id and data-action in jQuery
提问by timpone
This is a follow-up to this question: Using javascript:function syntax versus jQuery selector to make Ajax calls
这是这个问题的后续:Using javascript:function syntax vs jQuery selector to make Ajax calls
How would I select this fragment?
我将如何选择这个片段?
<div data-id="54" data-action="follow-global-id" class="add-to-list">here is my answer</div>
I have the id value below and tried this
我有下面的 id 值并尝试过这个
$('.add-to-list[data-action|="action-follow-global-id"][data-id|=id]').text('here is the things jtjt in the id value');
but no dice. Need to be AND'ing these together.
但没有骰子。需要将这些合并在一起。
thx for help
谢谢你的帮助
回答by Dmitry F
Didn't test it, but should work:
没有测试它,但应该工作:
var id = 54;
$('.add-to-list[data-action=follow-global-id][data-id='+id+']').text('something');
回答by Chris Pietschmann
This will work:
这将起作用:
$('.add-to-list[data-action="follow-global-id"][data-id="54"]').
text('here is the things jtjt in the id value');
Here's a full code example you can execute to test:
这是您可以执行以进行测试的完整代码示例:
<html>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.js"></script>
<script>
$(function(){
$('.add-to-list[data-action="follow-global-id"][data-id="54"]').
text('here is the things jtjt in the id value');
});
</script>
<div data-id="54" data-action="follow-global-id" class="add-to-list">here is my answer</div>
</html>
回答by Rusty Jeans
Is this what you're looking for?
这是你要找的吗?
$('.add-to-list[data-action|="follow-global-id"][data-id]').each(function (i) {
$(this).text('here is the things ' + $(this).attr('data-id') + ' in the id value');
});
回答by Travis Delly
all you need to do is
你需要做的就是
$("[data-action=value")
OR
或者
$("[data-id=value"])