也许我找得不好,但我找不到在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");