当用户单击按钮时,我尝试将空白输入字段添加到有序列表(id = myList)。我编写了以下代码来将列表元素添加到有序列表中,然后在该新列表元素中插入输入字段。当我执行此代码时,按下按钮时什么也没有发生。
关于我做错了什么有什么想法吗?
<script type="text/javascript">
function addToList()
{
var doc = document.getElementById("myList");
var newLI = document.createElement("li");
var input = document.createElement("input");
input.type = "text";
input.size = "60";
newLi.appendChild(input);
doc.appendChild(newLI);
}
</script>
最佳答案
拼写错误,是 newLI
,而不是 newLi
<script type="text/javascript">
function addToList()
{
var doc = document.getElementById("myList");
var newLI = document.createElement("li");
var input = document.createElement("input");
input.type = "text";
input.size = "60";
newLI.appendChild(input); // typo here, it's not newLi
doc.appendChild(newLI);
}
</script>
关于javascript - HTML 如何使用 javascript 函数将输入标记添加到有序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21460994/