[英]how do I input javascript created nodes into an array
I'm trying to push elements generated from this 我正在尝试推送由此产生的元素
function myFunction()
{
var x=document.getElementById("stat");
var para=document.createElement("p");
var node=document.createTextNode(stat.value);
para.appendChild(node);
var element=document.getElementById("output");
element.appendChild(para);
stat.value=""
}
into an array to run through 4 separate functions to determine their avg., med., mode, and standard deviation. 分为四个独立的函数,以确定它们的平均,中间,模式和标准偏差。 I thought I'd be about to target them with an getElementByTagName, but it doesnt seem to work.
我以为我要使用getElementByTagName来定位它们,但似乎不起作用。
It's a typo. 这是一个错字。
You're using stat
as a variable name instead of x
. 您使用
stat
作为变量名称,而不是x
。
The most important thing for a variable it's the name... you should pick variable names more carefully. 变量最重要的是名称...您应该更仔细地选择变量名称。
Functions have both a name and a semantic and the semantic (logic) is of course more important than the name (not saying that the name is not important, of course). 函数既具有名称又具有语义,并且语义(逻辑)当然比名称更重要(当然,并不是说名称并不重要)。
But variables are basically just names. 但是变量基本上只是名称。
Something like this? 像这样吗
function myFunction(element)
{
var stat=document.getElementById(element);
var para=document.createElement("p");
var node=document.createTextNode(stat.value);
para.appendChild(node);
var element=document.getElementById("output");
element.appendChild(para);
stat.value=""
}
var elements = ['stat', 'stat2', 'stat3', 'stat4']
for (var i = 0; i < elements.length; ++i) {
myFunction(elements[i])
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.