我应该如何使用HTMLOptionElement.Option()构造函数?

时间:2016-12-15 20:45:12

标签: javascript html5

我正在尝试向像这样的选择元素添加选项

<select id="select-title"></select>
<script>
  let s = document.querySelector('#select-title');
  let optionElementReference = new HTMLOptionElement.Option('option1');
  s.add(optionElementReference);
</script>

但是我得到了(Chrome 54)&#34; Uncaught TypeError:HTMLOptionElement.Option不是构造函数(...)&#34;

我认为我应该能够做到这一点,因为下面的文档(尽管我没有找到这种用法的例子)。我想我可能在某处犯了一个简单的错误。

https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement

HTMLOptionElement.Option()是构造函数,用于创建HTMLOptionElement对象。它有四个值:要显示的文本,文本,关联的值,值,defaultSelected的值以及selected的值。最后三个值是可选的。

1 个答案:

答案 0 :(得分:1)

删除let s = document.querySelector('#select-title'); let optionElementReference = new Option('option1'); s.add(optionElementReference);

&#13;
&#13;
<select id="select-title"></select>
&#13;
where
&#13;
&#13;
&#13;

相关问题