简体   繁体   中英

How to print javascript function in an HTML file?

I have a separate A.js file with a function that prints a value. I also have a separate HTML file B.html. Is there a way to print the value from A.js to B.HTML? Right now I have this in my A.js:

document.getElementById('sum').innerHTML = totSum;

This in my B.HTML inside the body tag: <script> <div id="sum"></div> </script>

You should call the script after the declaration of the sum div.

A.js :

document.getElementById('sum').innerHTML = totSum;

B.html :

[...]<div id="sum"></div> <script src='A.js'></script>[...]

Your code should look something like this:

<body>
<div id="sum"></div>


<script src="a.js"></script>
<script src="b.js"></script>

</body>

And of course, somewhere in the folder with your B.html file, you have a.js and b.js (or whatever you are calling them).

Make sure the function that is printing the sum is actually being called, and instead of having it return a value (or perhaps in addition to returning a value), have it change the value of the item on the page:

var sumView = document.getElementById("sum");
sumView.value = sumFunct(3,5);

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