检查是否存在特定的数据属性值

Check for existence of specific data attribute value

本文关键字:数据属性 是否 存在 检查      更新时间:2023-09-26

我正在尝试选择一个具有特定属性值的DOM元素。我想避免each()循环,但我在jQuery中看到的只是检测数据属性存在的能力,而不是它的值。所以,我想完成这样的事情:

if ($('.item[data-index="' + indexVal + '" ]'){
  //if an .item with the data-index value of indexVal exists...
}
不应将缺少的)$(selector)放入if条件中。它总是CCD_ 3,即使它没有选择任何东西。
if ($('.item[data-index="' + indexVal + '" ]').length) {

试试这个:

if ($('.item[data-index="' + indexVal + '" ]').length > 0){
}

应该是

if ($('.item[data-index="' + indexVal + '" ]')){
  //if an .item with the data-index value of indexVal exists...
}

if(jQuery('.item').attr('data-index') == indexVal){
}

$('.item[数据索引]').length>0

如果您搜索特定的值,您可以在"中插入该值:

$('.item[数据索引="123"]').length>0

$('.item[data index]').is('*')