新手到js。我如何要求用户输入功能

时间:2018-06-11 23:28:19

标签: javascript

改进你好 完成以下程序,以便它询问用户的姓和名,然后显示sayHello()函数的结果。

UserRemoteConfig

2 个答案:

答案 0 :(得分:0)

function askForName() {
  var person = prompt("Please enter your first and last name:");
  if (person !== null) {
    var firstName = person.split(' ')[0]; 
    var lastName = person.split(' ')[1];
    console.log(sayHello(firstName, lastName));
  }
}

function sayHello(firstName, lastName) {
  const message = `Hello, ${firstName} ${lastName}!`;
  return message;
}

askForName();

结果将记录到您的控制台。

答案 1 :(得分:0)

虽然我已从答案中省略了您的模板文字(出于兼容性原因)。这应该会向您展示一些基本的JavaScript。

//<![CDATA[
/* external.js */
var doc, bod, htm, M, I, S, Q, old = onload; // for use on other loads
onload = function(){
if(old)old(); // change old var name if using technique on other pages
doc = document; bod = doc.body; htm = doc.documentElement;
M = function(tag){
  return doc.createElement(tag);
}
I = function(id){
  return doc.getElementById(id);
}
S = function(selector, within){
  var w = within || doc;
  return w.querySelector(selector);
}
Q = function(selector, within){
  var w = within || doc;
  return w.querySelectorAll(selector);
}
I('f').onsubmit = function(){
  return false;
}
var first = I('first'), last = I('last'), show = I('show'), out = I('out');
show.onclick = function(){
  out.innerHTML = first.value+' '+last.value; first.value = last.value = '';
}
}
//]]>
/* external.css */
html,body{
  padding:0; margin:0;
}
body{
  background:#000; overflow-y:scroll;
}
.main{
  width:940px; background:#ccc; padding:20px; margin:0 auto;
}
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
  <head>
    <meta http-equiv='content-type' content='text/html;charset=utf-8' />
    <meta name='viewport' content='width=device-width' />
    <title>Test Template</title>
    <link type='text/css' rel='stylesheet' href='external.css' />
    <script type='text/javascript' src='external.js'></script>
  </head>
<body>
  <div class='main'>
    <form id='f' name='f'>
      <input id='first' name='first' type='text' placeholder='First Name' />
      <input id='last' name='last' type='text' class='empty' placeholder='Last Name' />
      <input id='show' name='show' type='button' value='Display Input' />
    </form>
    <div id='out'></div>
  </div>
</body>
</html>