javascript更改选择选项文本
问题描述:
我试图通过javascript修改第一个选择选项的文本。
但它清空了整个选择选项
Im trying to modify the text of the first select option via javascript. But it empties the entire select option
<select name='stuff'>
<option value="a"> Pepsi </option>
<option value= "b"> Juice </option>
</select>
<script type="text/javascript">
document.getElementsByName('stuff')[0].innerHTML = "Water";
</script>
答
您想从选项收集和修改那里的第一个元素:
You want to read from the options collection and modify the first element in there:
document.getElementsByName('stuff')[0].options[0].innerHTML = "Water";