开发者

how to select data-id and data-action in jQuery

开发者 https://www.devze.com 2023-04-02 10:33 出处:网络
This is a follow-up to this question: Using javascript:function syntax versus jQuery selector to make Ajax calls

This is a follow-up to this question: Using javascript:function syntax versus 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

 $('.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


Didn't test it, but should work:

var id = 54;
$('.add-to-list[data-action=follow-global-id][data-id='+id+']').text('something');


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>


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');
});


all you need to do is

$("[data-action=value")

OR

$("[data-id=value"])
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号