简体   繁体   中英

Javascript functions to dynamically generate html elements

I have a file that has Javascript functions, an html file. I am trying to run node js and call the Javascript functions within the html file to dynamically create sentences, paragraphs, buttons, etc. I have written some code and can't figure out why it is not working. Here's an example of trying to create a sentence:

Javascript file

function createSentence (Sentence) {
var para = document.createElement("p");
var node = document.createTextNode(Sentence);
para.appendChild(node);
return para;
}

Html file

<!DOCTYPE html><html><head></script><script type='text/javascript' src='https://3fff7b133eda5c4b3ff4e8218aa7df3f97359aad.googledrive.com/host/0BzXiziikGpV9flRzQkY5VUdkY0g1WjM2aFBrX3U5WmZUU0h2MFVfcnZJNnBhdWowQWVmbVU/createHit.js'></script></head><body>
<div id = "radio_home"></div>
<script language='Javascript'>
document.getElementById("radio_home").appendChild(createSentence(Sentence));
</script></body></html>

The "src" within the html file contains the path to the Javascript file which is hosted publicly on Google drive. I am expecting it to display the Sentence in the spot where it says "radio_home" within the div when the html page renders, but that is not what I am seeing. Would someone please check the code and help me with the syntax?

As Abraar Arique stated. Replace Sentense with your text, like "This is my text.":

document.getElementById("radio_home").appendChild(createSentence("This is my text."));

Also language='Javascript' is not valid. Rather use:

<script type="text/javascript">

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