嗨,我一直在检查我的代码,但我不知道为什么会发生错误。显然我没有从我的孩子中得到我的孩子价值观 tt列表。
<ul id = "ttList" style="list-style: none; padding-left: 0"></ul>
function creatTT()
{
var input = document.getElementById("integer").value;
var li;
var value;
if(/^[1-9]$/.test(input) == true)
{
for(var i = 1; i <= 9; i++)
{
li = document.createElement("li");
value = document.createTextNode(input + " x " + i + " = " + (input* i));
li.appendChild(value);
document.getElementById("ttList").appendChild(li);
}
}
var ttList = document.getElementById("ttList").childNodes[0];
ttList.replaceChild(input, ttList.childNodes[0]);
}
答案 0 :(得分:0)
您在replaceChild方法中使用Node的值,而不是Node本身。请尝试下面的代码。
<ul id = "ttList" style="list-style: none; padding-left: 0"></ul>
function creatTT()
{
var input = document.getElementById("integer").value;
var inputNode = document.getElementById("integer");
var li;
var value;
if(/^[1-9]$/.test(input) == true)
{
for(var i = 1; i <= 9; i++)
{
li = document.createElement("li");
value = document.createTextNode(input + " x " + i + " = " + (input* i));
li.appendChild(value);
document.getElementById("ttList").appendChild(li);
}
}
var ttList = document.getElementById("ttList").childNodes[0];
ttList.replaceChild(inputNode , ttList.childNodes[0]);
}
答案 1 :(得分:0)
将{{1}中的document.getElementById("integer")
替换为replacechild
的输入包含input
的值,而不是元素本身。 input element
期望元素被替换而不是值。
replacechild
function creatTT()
{
var input = document.getElementById('value').value;
var li;
var value;
if(/^[1-9]$/.test(input) == true)
{
for(var i = 1; i <= 9; i++)
{
li = document.createElement("li");
value = document.createTextNode(input + " x " + i + " = " + (input* i));
li.appendChild(value);
document.getElementById("ttList").appendChild(li);
}
}
var ttList = document.getElementById("ttList").childNodes[0];
ttList.replaceChild(document.getElementById('value'), ttList.childNodes[0]);
}
creatTT()