如何修复问题表和选择?

我的函数有问题,它给了我数据前面的空间。我哪里错了。如果根据这个例子你可以帮助我做到这一点,请告诉我。我想从送货单表中返回数据进行更正,但是空间有问题,如何解决。


鳕鱼


<!DOCTYPE html>

<html>

<body>


<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>




<script>

function myFunction(td) {


selectedRow = td.parentElement.parentElement;

document.getElementById("mySelect").value= selectedRow.cells[1].innerHTML;



}

</script>


</body>

</html>


米脂
浏览 79回答 1
1回答

红糖糍粑

每当您尝试从 HTML 执行此操作时,请尝试使用trim()多余的空格,因为.innerHTMLHTML 不是空格敏感的,但 JavaScript 是:document.getElementById("mySelect").value = selectedRow.cells[1].innerHTML.trim();//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^^^^这将使它发挥作用。片段在这里:function myFunction(td) {&nbsp; selectedRow = td.parentElement.parentElement;&nbsp; document.getElementById("mySelect").value = selectedRow.cells[1].innerHTML.trim();}<form>&nbsp; Select your favorite fruit:&nbsp; <table>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; orange&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" onclick="myFunction(this)">Try it</button>&nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; </tr>&nbsp; </table>&nbsp; <select id="mySelect">&nbsp; &nbsp; <option value="apple">Apple</option>&nbsp; &nbsp; <option value="orange">Orange</option>&nbsp; &nbsp; <option value="pineapple">Pineapple</option>&nbsp; &nbsp; <option value="banana">Banana</option>&nbsp; </select></form>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript