我有一个元素集合,我想根据所有元素都是类型的条件对其进行过滤。
例如。
$('.container').children('type[:select]')
我知道上面的代码无效,但我想要类似的东西,我想要所有具有类型的元素。
请原谅我的 js 代码,还是新手!
最佳答案
几乎与您使用的完全一样,除了 select
是一个元素
var $selects = $('.container').children('select');
或
var $selects = $('.container').find('select');
两者之间的区别是:
the .children() method differs from .find() in that .children() only travels a single level down the DOM tree while .find() can traverse down multiple levels to select descendant elements (grandchildren, etc.) as well.
来自:jQuery.children
documentation
您也可以在 1 个选择器中完全完成此操作 - 对于直系后代(类似于上面的 children
)
var $selects = $('.container > select');
或任何后代
var $selects = $('.container select');
关于javascript - 如何根据类型过滤元素(<select>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38375141/