在没有jQuery用于选择所有下拉列表的情况下,相当于$(“select”)

What is equivalent of $("select") without jQuery for selecting all dropdown?

本文关键字:相当于 select 用于 jQuery 选择 下拉列表 情况下      更新时间:2023-09-26

也许我找得不好,但我找不到在dom中选择所有下拉列表的普通javascript方法。

在jQuery中,我可以使用

var allDropdowns = $("select") ;

你知道纯javascript中的等价物吗?

为了获得最佳的"相似"体验,如果您的浏览器兼容级别允许,我建议使用document.querySelectorAll

console.log(document.querySelectorAll('select'));
<select id="a"><option>1</option></select>
<select id="b"><option>1</option></select>
<p id="whatever"></p>

对于IE8,你应该小心,而IE7,你需要一个polyfill。对于几乎所有其他情况,你应该没事。

document.querySelector('select')

如果不止一个:

document.querySelectorAll('select')
var allDropdowns = document.getElementsByTagName("select");