📜  javascript 选项 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:01.557000             🧑  作者: Mango

代码示例1
/* assuming we have the following HTML

*/ 

var s = document.getElementById('s');
var options = [ 'zero', 'one', 'two' ];

options.forEach(function(element, key) {
  if (element == 'zero') {
    s[s.options.length] = new Option(element, s.options.length, false, false);
  }                
  if (element == 'one') {
    s[s.options.length] = new Option(element, s.options.length, true, false); // Will add the "selected" attribute    
  }
  if (element == 'two') {
    s[s.options.length] = new Option(element, s.options.length, false, true); // Just will be selected in "view"
  }
});

/* Result

*/