我是靠谱客的博主 激昂鞋垫,这篇文章主要介绍JavaScript关于select的相关操作说明,现在分享给大家,希望可以做个参考。

一、 插入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的相关操作说明的全部内容,更多相关JavaScript关于select内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(197)

评论列表共有 0 条评论

立即
投稿
返回
顶部