源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JavaScript关于select的相关操作说明

  • 时间:2020-09-05 20:57 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JavaScript关于select的相关操作说明
[b]一、 插入option[/b] 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)); [b]二、 清空option [/b]var oSelectMonth = document.getElementById("SelectMonth"); oSelectMonth.options.length = 0; //清空Select里面的options [b]三、 设置默认选中option[/b] 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来设置
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部