1. 程式人生 > >動態生成的option 並插入select標籤的第一個,且預設選中

動態生成的option 並插入select標籤的第一個,且預設選中

   html:

        <select id="scene">             <option value="111">111</option>             <option value="222">222</option>         </select>

js:

  var first=document.getElementById("scene").firstChild; //得到第一個元素    var option = document.createElement("option");   $(option).val("newScene");   $(option).text("newScene");      document.getElementById(scene").insertBefore(option,first);

$("#scene").find("option[value='newScene']").attr("selected",true);//預設選中