jQuery如何获取没有特定属性的元素 [英] How can I get the elements without a particular attribute by jQuery
本文介绍了jQuery如何获取没有特定属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何获取具有特定属性的元素:
I know how to get elements with a particular attribute:
$("#para [attr_all]")
但是,如何在没有特定属性的情况下获取元素? 我尝试
But how can I get the elements WITHOUT a particular attribute? I try
$("#para :not([attr_all])")
但是它不起作用. 正确的方法是什么?
But it doesn't work. What is the correct way to do this?
让我举个例子:
<div id="para">
<input name="fname" optional="1">
<input name="lname">
<input name="email">
</div>
jQuery:
$("#para [optional]") // give me the fname element
$("#para :not([optional])") //give me the fname, lname, email (fname should not appear here)
推荐答案
我想到的第一件事(也许是次优的):
First thing that comes to my mind (maybe sub optimal) :
$('p').filter(function(){
return !$(this).attr('attr_all');
});
但是p:not([attr_all])
应该可以工作,所以我认为您的代码中还有其他情况.
However p:not([attr_all])
should work, so I think something else is going on in your code.
这篇关于jQuery如何获取没有特定属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文