1 回答

TA貢獻1815條經(jīng)驗 獲得超6個贊
每當(dāng)您嘗試從 HTML 執(zhí)行此操作時,請嘗試使用trim()多余的空格,因為.innerHTMLHTML 不是空格敏感的,但 JavaScript 是:
document.getElementById("mySelect").value = selectedRow.cells[1].innerHTML.trim();
// ^^^^^^^
這將使它發(fā)揮作用。片段在這里:
function myFunction(td) {
selectedRow = td.parentElement.parentElement;
document.getElementById("mySelect").value = selectedRow.cells[1].innerHTML.trim();
}
<form>
Select your favorite fruit:
<table>
<tr>
<td>
1
</td>
<td>
orange
</td>
</td>
<td>
<button type="button" onclick="myFunction(this)">Try it</button>
</td>
</tr>
</table>
<select id="mySelect">
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="pineapple">Pineapple</option>
<option value="banana">Banana</option>
</select>
</form>
添加回答
舉報