2016-07-14 57 views
0

我有一个元素的集合,我想根据条件来过滤它,它是所有类型的元素。如何过滤基于类型的元素(<select>)

例如,

$('.container').children('type[:select]') 

我知道上面的代码是无效的,但我想要类似的东西,我想要所有类型的元素。

请原谅我的js代码,仍然是新手!

+1

'$( '集装箱')找到( '选择')' –

+0

寻找这一个http://jsfiddle.net/FvMYz/? –

+1

阅读有关选择器的文档https://api.jquery.com/category/selectors/ – epascarello

回答

2

几乎正是你使用什么,除了select是一个元素

var $selects = $('.container').children('select'); 

var $selects = $('.container').find('select'); 

而2之间的区别是:

的。儿童()方法与.find()的不同之处在于.children()只向DOM树中的单个级别传递,而.find()可以遍历多个lev els选择后代元素(孙辈等)。

来源:jQuery.children documentation

你也可以这样做完全是在1个选择 - 直接后代(类似于上述children

var $selects = $('.container > select'); 

或任何后代

var $selects = $('.container select'); 
0

你可以试试这样的find()

$('container').find('select') 
0

没有类型就像selectselect是一个HTML元素:

$('.container').children('select') 
$('.container > select') 

我想和类型的所有元素

这混淆我。可能是你想要得到的所有元素具有type属性:

$('.container').children('[type]') // if direct child 

使用.find()

$('.container').find('[type]') // if not a direct child