jQuery:找到具有特定自定义属性的元素 [英] jQuery: Find the element with a particular custom attribute

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

问题描述

我只想找到具有自定义属性特定值的元素。

I just want to find the element with a particular value for a custom attribute.

例如。我想找一个 div ,它的属性为 data-divNumber =6

E.g. I want to find a the div which has the attribute data-divNumber="6".

var number = 6;
var myDiv = $('[data-divNumber = number]');

我尝试使用 http://api.jquery.com/attribute-equals-selector/ 但我所做的并不起作用。

I tried using http://api.jquery.com/attribute-equals-selector/ but what I've done doesn't work.

该属性只有一个具有该特定值的元素。

There is precisely one element with that particular value for the attribute.

推荐答案

var number = 6;
var myDiv = $('div[data-divNumber="' + number + '"]');

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

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