我一直在尝试找出如何删除特定的“li”标签,方法是根据其在列表中的位置输入其编号(即在文本框中输入 3,然后按按钮删除列表位置 3 中的项目) )。现在我的代码是这样的,即使只列出了 4 个项目,输入 5 并按 Enter 键仍然会删除 2 个项目,而不是不删除任何项目。我的代码编写方式与删除特定“li”标签时键入的数字不一致。
任何帮助将不胜感激,谢谢。
<html>
<head>
<title>Chapter 5 Activity</title>
</head>
<body>
<h1>The Best Fruit in the World</h1>
<ol id="fruit">
<li>Mangos</li>
<li>Watermelons</li>
<li>Kiwis</li>
<li>Pineapples</li>
</ol>
<form action="">
<input type="text" name="rfruit" id="fruitremove">
<input type="button" value="Click to Remove" onclick="removefruit()">
Remove fruit you dislike
</form>
<script type="text/javascript">
function removefruit(){
var fruitminus = document.getElementById("fruitremove").value;
var flist = document.getElementById("fruit");
flist.removeChild(flist.childNodes[fruitminus]);
}
</script>
</body>
</html>
慕尼黑8549860
相关分类