简体   繁体   中英

Transferring data from JavaScript in the head to html in the body

I'm having a problem here trying to transfer some information from the head of my html file, which is in JavaScript, to a table I want to create in a div in the body. I want to know if it's possible to pass information from the head to the body. Say, to create a table and have the input data be some declared variables in the head.

I have a function also and I would like to transfer output from that function and input it into the table. Here is some code that I have.

<html>
<head>
    <script language="JavaScript">


        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 = funciton(x1, y1, R, x2, y2) {
            return (Math.sqrt(Math.pow((y2 - y1), 2) + Math.pow((x2 - x1), 2))) - R;
        };

    </script>
</head>
<body>
    <div>



    </div>
</body>

You misspelled "function". Try this:

var distance = function() {};

Yes, you can assign the return value of that function to an input inside a table. Say you have the following input (I didn't see one in your markup, this is all example):

<input type="text" id="tableInput"/>

You can then set the value of this input:

document.getElementById("tableInput").value = distance();

 function loadInTable() { var checkData=[ [1,1,2,3], [4,5,6,7], [8,9,10,11], [12,13,14,15], ]; for(var i=0;i<4;i++) { for(var j=0;j<4;j++){ id=i+""+j; document.getElementById(id).innerHTML =checkData[j][i]; } } } 
 table,tr,td{ border:1px solid black; } table{ width:50%; } 
 <body onload="loadInTable()"> <center><table > <tr><td id="00"></td><td id="10"></td><td id="20"></td><td id="30"></td></tr> <tr><td id="01"></td><td id="11"></td><td id="21"></td><td id="31"></td></tr> <tr><td id="02"></td><td id="12"></td><td id="22"></td><td id="32"></td></tr> <tr><td id="03"></td><td id="13"></td><td id="23"></td><td id="33"></td></tr> </table></center> </body> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM