在JavaScript中使用滴列表选择器
如果我知道滴列表的ID,我可以使用userscript通过javascript选择滴列表,但是如果滴列表没有ID,我就不能选择它,所以我想知道是否有一种方法可以选择页面上的所有滴列表,而不使用ID在JavaScript中使用滴列表选择器,javascript,selector,userscripts,Javascript,Selector,Userscripts,如果我知道滴列表的ID,我可以使用userscript通过javascript选择滴列表,但是如果滴列表没有ID,我就不能选择它,所以我想知道是否有一种方法可以选择页面上的所有滴列表,而不使用ID document.getElementById("id").selectedIndex = 0; 选择全部 const all=document.queryselectoral('select'); 选择第一个 const first=document.querySelector('select'
document.getElementById("id").selectedIndex = 0;
选择全部
const all=document.queryselectoral('select');
选择第一个
const first=document.querySelector('select');
console.log(第一个.selectedIndex);
编辑: 在这里,您可以看到一个示例,它如何循环多个选择框并设置selectedIndex(在我的示例中为3)
const all=document.queryselectoral('select');
[…全部].forEach(select=>select.selectedIndex=3)代码>
沃尔沃汽车
萨博
欧宝
奥迪
1.
2.
3.
4.
您可以发布HTML吗?那么这其中的哪一部分实际上选择了droplist?因此,我是否必须更改为使其类似于:document.querySelectorAll('select')。selectedIndex=0?这是部分document.querySelector('select')
,并提供第一个选择框。在这里,您可以使用document.querySelector('select')。selectedIndex=0
和所有选择器document.queryselectoral('select')
一起使用,您将获得一个节点列表(类似数组)。在这里,您必须转换数组中的节点列表并抛出循环以设置所选的索引。您可以编写一个循环示例,了解如何执行“全部”操作吗?谢谢!工作完美。