我试图通过javascript修改第一个选择选项的文本。 但它会清空整个选择选项
<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>
答案 0 :(得分:17)
您想要从options集合中读取并修改其中的第一个元素:
document.getElementsByName('stuff')[0].options[0].innerHTML = "Water";
答案 1 :(得分:2)
你可以试试这个:
$('select[name=stuff] option:first').html("abcd");
答案 2 :(得分:0)
我认为这应该有效:
<select name='stuff'>
<option id="first" value="a"> Pepsi </option>
<option value= "b"> Juice </option>
</select>
<script type="text/javascript">
document.getElementById("first").innerHTML = "Water";
</script>