一、 插入option
1、DOM方法
var oSelectYear = document.getElementById("SelectYear");
var op = document.createElement("option");
op.innerHTML = "2010";
op.value = "2010";
oSelectYear.appendChild(op);

2、new Option方法
var oSelectMonth = document.getElementById("SelectMonth");
oSelectMonth.options.add(new Option(1, 1));
oSelectMonth.options.add(new Option(2, 2));

二、 清空option
var oSelectMonth = document.getElementById("SelectMonth");
oSelectMonth.options.length = 0; //清空Select里面的options

三、 设置默认选中option
var oSelectMonth = document.getElementById("SelectMonth");
//oSelectMonth.selectedIndex = 1; //方法一:默认选中第二项
//setTimeout(function() { oSelectMonth.selectedIndex = 1; }, 0); //用setTimeout延迟是为了防止dom渲染问题
// oSelectMonth.options[1].selected = true; //方法二
oSelectMonth.options[1].setAttribute("selected", "true"); //方法三:比较推荐的用setAttribute来设置

以上就是【JavaScript关于select的相关操作说明】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)

与本文相关的软件

发表我的评论

最新评论

  1. 暂无评论