JavaScript - 名字输入和姓氏输入,然后用(欢迎"lastname" "Error processing tar file(exit status 1): operation not sup



我可以想象答案很简单,谁能看看我的代码。它应该是两个输入框,有一个弹出框说(欢迎"名字"last-name"

function welcomeTheUsar() {
// Some code borrowed and rewritten from UNKNOWN's lessons
let firstName = document.getElementById("first-name").value;
let lastName = document.getElementById("last-name").value;
let fullName = "first-name" + "last-name".value;
console.log(fullName);
alert("Welcome " + "fullname");
}
<!-- Make my name in alternating colors for each of the letters-->
<h1>Cora</h1>
<div id="welcomeTheUsar">
<!--This is the welcome div for the user, code also borrowed and moddified from UNKNOWN's lessons-->
<input placeholder="Enter First Name" id="first-name">
<input placeholder="Enter Last Name" id="last-name">
<button onclick="welcomeTheUsar()">Greetings</button>
</div>

您不能在像这样的字符串中使用变量名&;variablename &;,它将被解释为纯文本。应该是这样的:

<script>
function welcomeTheUsar() {
// Some code borrowed and rewritten from UNKNOWN's lessons
let firstName = document.getElementById("first-name").value; 
let lastName= document.getElementById("last-name").value;
let fullName= `${firstName} ${lastName}`;
console.log(fullName);
alert("Welcome "+fullName);
}
</script>
<!-- Make my name in alternating colors for each of the letters-->
<h1>Cora</h1>
<body>
<div id="welcomeTheUsar">
<!--This is the welcome div for the user, code also borrowed and moddified from UNKNOWN's lessons-->
<input placeholder="Enter First Name" id="first-name"> 
<input placeholder="Enter Last Name" id="last-name"> 
<button onclick="welcomeTheUsar()">Greetings</button> 
</div>

答案应该是:

function welcomeTheUsar() {
// Some code borrowed and rewritten from UNKNOWN's lessons
let firstName = document.getElementById("first-name").value; 
let lastName= document.getElementById("last-name").value;
let fullName=firstName+ " " + lastName;
console.log(fullName);
alert("Welcome "+ fullName);
}

您使用的不是给定的变量,而是看起来像变量的文本字符串。

这个不起作用,因为它只是文本,并且您试图获取字符串"last-name"的值。

let fullName="first-name"+"last-name".value;

这个会显示"Welcome fullname">

alert("Welcome "+"fullname");

当你在某些东西周围使用引号时,它被视为文本而不是变量。

function welcomeTheUsar() {
const
firstName = document.getElementById("first-name").value,
lastName = document.getElementById("last-name").value;
alert(`Welcome ${firstName} ${lastName}`);
}
<div id="welcomeTheUsar">
<input placeholder="Enter First Name" id="first-name">
<input placeholder="Enter Last Name" id="last-name">
<button onclick="welcomeTheUsar()">Greetings</button>
</div>

相关内容

最新更新