我试图通过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>
你想阅读 选项 收集并修改那里的第一个元素:
document.getElementsByName('stuff')[0].options[0].innerHTML = "Water";
你可以试试这个:
$('select[name=stuff] option:first').html("abcd");
我认为这应该有效:
<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>