JavaScript操作select下拉框

1.新建一个select.html

1 <html><head></head><body><select id="1">
2   <option value="东风本田">东风本田</option>
3   <option value="广汽本田">广汽本田</option>
4   <option value="一汽大众">一汽大众</option>
5   <option value="上海大众">上海大众</option>
6 </select></body></html>

 2.js操作打开下拉框

1 function openselect(){
2 var select1=document.getElementById(1);
3 var selectlength = select1.length;
4 select1.size = selectlength;
5 select1.style = "height:100%";
6 return ''}
7 openselect();

 

 

 

 3.选择其中一个选项

1 function selectone(){
2 var selectoption = document.evaluate('//*[@id="1"]/option[3]', document).iterateNext();
3 selectoption.parentNode.size = 1;
4 selectoption.parentNode.style = "none";
5 selectoption.selected = true;
6 return '';}
7 selectone();

 4.执行事件

let evt = document.createEvent('HTMLEvents'); 
evt.initEvent('change', false, true);//页面的下拉框change事件
selectoption.parentNode.dispatchEvent(evt);

 

posted @ 2021-11-02 09:59  新*  阅读(1785)  评论(0编辑  收藏  举报