简体   繁体   中英

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".

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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