jQuery按数据属性值查找元素 [英] jQuery find element by data attribute value

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

问题描述

我有一些如下所示的元素:

I have a few elements like below:

<a class="slide-link" href="#" data-slide="0">1</a>
<a class="slide-link" href="#" data-slide="1">2</a>
<a class="slide-link" href="#" data-slide="2">3</a>

如何向具有0(零)的data-slide属性值的元素添加一个类?

How can I add a class to the element which has a data-slide attribute value of 0 (zero)?

我尝试了许多不同的解决方案,但是没有任何效果.一个例子:

I have tried many different solutions but nothing worked. An example:

$('.slide-link').find('[data-slide="0"]').addClass('active');

有什么主意吗?

推荐答案

使用属性等于选择器

$('.slide-link[data-slide="0"]').addClass('active');

小提琴演示

.find()

它在树上工作

获取每个元素的后代 在当前匹配的元素集中,由选择器,jQuery对象或元素过滤.

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

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

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