简体   繁体   English

如何接受javascript输入并在html中调用它?

[英]how to take javascript input and call it in html?

I'm still new to writing javascript code and calling it into html. 我对编写javascript代码并将其调用为html还是陌生的。 I'm writing code for my personal website that I'm going to create. 我正在为我要创建的个人网站编写代码。 When a visitor enters my site, I'm prompting them for 3 pieces of information: name, company, and title. 当访问者进入我的网站时,我会提示他们输入3条信息:名称,公司和标题。 The Javascript looks like: Javascript看起来像:

<script>
var name =.prompt("please enter your name") 
var company =.prompt("please enter your company")
var title =.prompt("please enter your title")
</script>

What I'm looking to do is call these variables in the html so it creates a personalized home page for the user. 我想做的是在html中调用这些变量,以便为用户创建个性化主页。 How would I be able to pull this information into the html? 我如何将这些信息提取到html中? I'm assuming it would be something along the lines of using the input tag and calling the variables but not quite sure how to do that. 我假设这将是使用输入标签并调用变量的过程,但是还不确定如何做到这一点。 Thanks for your help. 谢谢你的帮助。

是的,您需要创建HTML,并在提示用户输入值之后,通过JavaScript在HTML标记中分配这些值。

try this, 尝试这个,

<html>
  <body>

<p>Click the button to demonstrate the prompt box.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

    <script>
       function myFunction() {
       var name =prompt("please enter your name") 
       var company =prompt("please enter your company")
       var title=prompt("please enter your title")


    if (name != null) {
        document.getElementById("demo").innerHTML =
        name + company + title;
        }
      }
   </script>

  </body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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