简体   繁体   English

如何检查是否存在至少一个带attr = something的li元素?

[英]How to check if there exists at least one li element with attr = something?

So I want to check if there is any li element that has a attribute of "data-foo" and its value is "bar". 因此,我想检查是否有一个li元素,其属性为“ data-foo”,其值为“ bar”。

Would this work? 这行得通吗? :

if ($('li[data-foo=bar]'))
{
//exists
}
else
{
//does not exist
}

Thanks 谢谢

Get length of elements retured by selector if it is greater then zero then it means you have atleast one element with provided condition in selector. 如果选择器确定的元素length greater zero则获取该元素的length ,这意味着选择器中atleast one具有提供条件的元素。

if ($('li[data-foo=bar]').length > 0)
{
//exists
}
else
{
//does not exist
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM