2016-07-14 55 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