在html表中记录多个JavaScript函数数据输出

在html表中记录多个JavaScript函数数据输出,javascript,html,function,html-table,getelementbyid,Javascript,Html,Function,Html Table,Getelementbyid,多亏了这个网站上的一位用户,我才能够将JavaScript函数中的一些数据记录到html表中。现在我还有一个问题要问。如何在页面加载时多次运行该函数,并将该函数的输出记录到同一个html表中?因为到目前为止,我的代码只运行并记录函数的输出一次,当我刷新页面时,我丢失了以前生成的数字。我希望能够运行该函数10次,并将其显示在html表中。非常感谢你的帮助 <html> <head> </head> <body> <scr

多亏了这个网站上的一位用户,我才能够将JavaScript函数中的一些数据记录到html表中。现在我还有一个问题要问。如何在页面加载时多次运行该函数,并将该函数的输出记录到同一个html表中?因为到目前为止,我的代码只运行并记录函数的输出一次,当我刷新页面时,我丢失了以前生成的数字。我希望能够运行该函数10次,并将其显示在html表中。非常感谢你的帮助

<html>
<head>
</head>
    <body>
        <script type="text/javascript" language="javascript">
            R = 10;
            theta = Math.random() * 2 * Math.PI;
            r1 = Math.random() * R;
            r2 = R + (Math.random() * 100);
            ray = r1 + (Math.random() * (r2 - r1));
            point = [ray*Math.cos(theta), ray*Math.sin(theta)];


            var distance = function(x1, y1, rad, x2, y2) {
                return (Math.sqrt(Math.pow((y2 - y1), 2) + Math.pow((x2 - x1), 2))) - rad;
            };

        </script>

    <div>
            <table border="1">
                <th>X Center</th>
                <th>Y Center</th>
                <th>r1</th>
                <th>r2</th>
                <th>X Random</th>
                <th>Y Random</th>
                <th>Distance</th>
                <tr>
                    <td><input type="text" id="x center"/></td>
                    <td><input type="text" id="y center"/></td>
                    <td><input type="text" id="r1"/></td>
                    <td><input type="text" id="r2"/></td>
                    <td><input type="text" id="x random"/></td>
                    <td><input type="text" id="y random"/></td>
                    <td><input type="text" id="displacement"/></td>
                </tr>

            </table>
        <script>
            document.getElementById("x center").value = 0;
            document.getElementById("y center").value = 0;
            document.getElementById("r1").value = r1;
            document.getElementById("r2").value = r2;
            document.getElementById("x random").value = point[0];
            document.getElementById("y random").value = point[1];
            document.getElementById("displacement").value = distance(0,0,R,point[0], point[1]);
        </script>
    </div>
  </body>
</html>

R=10;
θ=Math.random()*2*Math.PI;
r1=Math.random()*R;
r2=R+(数学随机()*100);
ray=r1+(Math.random()*(r2-r1));
点=[ray*Math.cos(θ),ray*Math.sin(θ)];
var距离=函数(x1、y1、rad、x2、y2){
返回(数学sqrt(数学功率((y2-y1),2)+数学功率((x2-x1),2))-rad;
};
X中心
Y中心
r1
r2
X随机
Y随机
距离
document.getElementById(“x中心”).value=0;
document.getElementById(“y中心”).value=0;
document.getElementById(“r1”).value=r1;
document.getElementById(“r2”).value=r2;
document.getElementById(“x随机”).value=点[0];
document.getElementById(“y随机”).value=点[1];
document.getElementById(“位移”).value=距离(0,0,R,点[0],点[1]);

这就是您想要的:

<html>

    <head></head>

    <body>
        <div>
            <table border="1">
                <thead>
                    <tr>
                        <th>X Center</th>
                        <th>Y Center</th>
                        <th>r1</th>
                        <th>r2</th>
                        <th>X Random</th>
                        <th>Y Random</th>
                        <th>Distance</th>
                    </tr>
                </thead>
                <tbody id="log"></tbody>
            </table>
        </div>
        <script>
            for (var i = 0; i < 10; i++) {
                addRow();
            }

            function addRow() {
                var R = 10;
                var theta = Math.random() * 2 * Math.PI;
                var r1 = Math.random() * R;
                var r2 = R + (Math.random() * 100);
                var ray = r1 + (Math.random() * (r2 - r1));
                var point = [ray * Math.cos(theta), ray * Math.sin(theta)];
                var dist = distance(0, 0, R, point[0], point[1]);

                var newRow =
                    "<tr>" +
                    "<td>" + 0 + "</td>" +
                    "<td>" + 0 + "</td>" +
                    "<td>" + r1 + "</td>" +
                    "<td>" + r2 + "</td>" +
                    "<td>" + point[0] + "</td>" +
                    "<td>" + point[1] + "</td>" +
                    "<td>" + dist + "</td>" +
                    "</tr>";
                document.getElementById("log").innerHTML += newRow;
            }

            function distance(x1, y1, rad, x2, y2) {
                return (Math.sqrt(Math.pow((y2 - y1), 2) + Math.pow((x2 - x1), 2))) - rad;
            };
        </script>
    </body>

X中心
Y中心
r1
r2
X随机
Y随机
距离
对于(变量i=0;i<10;i++){
addRow();
}
函数addRow(){
var R=10;
var theta=Math.random()*2*Math.PI;
var r1=Math.random()*R;
var r2=R+(Math.random()*100);
var-ray=r1+(Math.random()*(r2-r1));
var point=[ray*Math.cos(θ),ray*Math.sin(θ)];
var dist=距离(0,0,R,点[0],点[1]);
var newRow=
"" +
"" + 0 + "" +
"" + 0 + "" +
“”+r1+“”+
“+r2+”+
“”+点[0]+“”+
“”+点[1]+“”+
“”+dist+“”+
"";
document.getElementById(“log”).innerHTML+=newRow;
}
功能距离(x1,y1,rad,x2,y2){
返回(数学sqrt(数学功率((y2-y1),2)+数学功率((x2-x1),2))-rad;
};

如果函数运行10次,是否需要一个包含10行的表?或者仅仅是最后一个值?是的,十行对应于您一直覆盖同一元素的十次函数。您需要在表中添加一个新的
tr
,其中包含所有
td
s。否,代码按照我的要求运行。所有这些单元格都在同一行中,因为它们都来自同一个事件。您只调用了一次事件,并且设置了相同字段的值。非常感谢,Balint。我想我可以使用for循环,但我不确定在使用html时这是否可行。