如何使用JavaScript选择HTML ``元素的选项
可能重复: 如何使用JavaScript选择HTML元素的选项?类似以下内容:如何使用JavaScript选择HTML ``元素的选项,javascript,Javascript,可能重复: 如何使用JavaScript选择HTML元素的选项?类似以下内容: var selectBox = document.getElementById('selectbox'); selectBox.children[1].selected = true; 完整示例:我的快速示例: <select multiple id="select"> <option value="1">1</option> <option val
var selectBox = document.getElementById('selectbox');
selectBox.children[1].selected = true;
完整示例:我的快速示例:
<select multiple id="select">
<option value="1">1</option>
<option value="2" class="toselect">2</option>
<option value="3">3</option>
<option value="4" class="toselect">4</option>
<option value="5">5</option>
</select>
<script type="text/javascript">
var select = document.getElementById("select");
var select_options = select.getElementsByClassName("toselect");
for (var i = 0; typeof(select_options) != "undefined"; i++) {
select_options[i].selected = true;
}
</script>
在这里,我使用类名来指定需要选择哪些选项。你可以用你想要的任何东西。对于反对者,请不要说这不是我的原创,而是一个经过编辑的问题。