Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
所以我想检查是否有任何 li 元素具有“data-foo”属性并且其值为“bar”。
这行得通吗?:
if ($('li[data-foo=bar]')) { //exists } else { //does not exist }
谢谢
获取length选择器返回的元素,如果是greater,则zero意味着您atleast one在选择器中有提供条件的元素。
length
greater
zero
atleast one
if ($('li[data-foo=bar]').length > 0) { //exists } else { //does not exist }