简体   繁体   中英

How to take input in a page using javascript and display the input in the html?

I am trying to make a simple web page, and I want to take input from the user and display the same in the web page, how should I write the Javascript code for the output ? Currently it looks like this

var username = prompt("Your username")
var comment = prompt("Your comment")
println(username + comment)

You can use the document API (this API represents any web page loaded in the browser and serves as an end point into the web page's content, which is the DOM tree) for that and fetch one of the elements in your html file. then you can assign its innerText to the answers you got.

For example (using the getElementById )

 var username = prompt("Your username"); var comment = prompt("Your comment"); document.getElementById("answer").innerText = username + ' ' + comment;
 <div id="answer"> </div>

More about the document interface here

不确定您要查找的确切内容,但您可以使用以下命令编辑 HTML 值:

document.getElementbyId('id').innerHTML = ''; 

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