JavaScript關(guān)于select的相關(guān)操作說明_表單特效
來源:懂視網(wǎng)
責(zé)編:小采
時間:2020-11-27 20:46:51
JavaScript關(guān)于select的相關(guān)操作說明_表單特效
JavaScript關(guān)于select的相關(guān)操作說明_表單特效:一、 插入option 1、DOM方法 var oSelectYear = document.getElementById(SelectYear); var op = document.createElement(option); op.innerHTML = 2010; op.value = 2010; oSelectYear.ap
導(dǎo)讀JavaScript關(guān)于select的相關(guān)操作說明_表單特效:一、 插入option 1、DOM方法 var oSelectYear = document.getElementById(SelectYear); var op = document.createElement(option); op.innerHTML = 2010; op.value = 2010; oSelectYear.ap

一、 插入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
三、 設(shè)置默認(rèn)選中option
var oSelectMonth = document.getElementById("SelectMonth");
//oSelectMonth.selectedIndex = 1; //方法一:默認(rèn)選中第二項
//setTimeout(function() { oSelectMonth.selectedIndex = 1; }, 0); //用setTimeout延遲是為了防止dom渲染問題
// oSelectMonth.options[1].selected = true; //方法二
oSelectMonth.options[1].setAttribute("selected", "true"); //方法三:比較推薦的用setAttribute來設(shè)置
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
JavaScript關(guān)于select的相關(guān)操作說明_表單特效
JavaScript關(guān)于select的相關(guān)操作說明_表單特效:一、 插入option 1、DOM方法 var oSelectYear = document.getElementById(SelectYear); var op = document.createElement(option); op.innerHTML = 2010; op.value = 2010; oSelectYear.ap