jQuery查找具有特定数据属性的元素 [英] Jquery Find element with specific data attribute

查看:90
本文介绍了jQuery查找具有特定数据属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用什么函数来选择class ="rate_stars"的子div.

What function can I use to select child div of class="rate_stars".

我的可变范围是1-5,我想选择具有该特定数据评级的child div.

I have a variable range from 1 - 5 and I want to select child div with that specific data-rating.

我尝试过

 $('rate_widget').find().attr('data-rating', info).prevAll().andSelf().addClass('ratings_vote');

但这没用.

<div id="r1" class="rate_stars"
                             data-post-id="<?php echo $post_id ?>"
                             data-user-id="<?php echo $user_id;  ?>"
                             data-nonce="<?php echo $nonce ?>"
                            >
    <div data-rating="1" class="ratings_stars"></div>
    <div data-rating="2" class="ratings_stars"></div>
    <div data-rating="3" class="ratings_stars"></div>
    <div data-rating="4" class="ratings_stars"></div>
    <div data-rating="5" class="ratings_stars"></div>
    <div class="total_votes">vote data</div>
</div>

推荐答案

您可以使用jQuery中的属性选择器:

You can use the attribute selector found in jQuery:

$('#r1').find('[data-rating="' + info + '"]')

参考:所有jQuery属性选择器的列表

这篇关于jQuery查找具有特定数据属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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