Javascript .createTextNode 输出给出错误
Javascrit .createTextNode output is giving errors
我创建了一个网站,上面会显示您的年龄。我使用 document.createTextNode 来存储输出,但输出无法正常工作。这是输出代码
var h1 = document.createElement("p");
h1.setAttribute("id", "mainText")
var mainText = document.createTextNode("You are ", ageYears, " years, ", ageMonths, "
months and ", ageDays, " days old.");
h1.appendChild(mainText);
document.getElementById("new-age").appendChild(h1);
当我 运行 我的代码时,它只输出第一部分,“你是”。有什么办法可以输出整条消息吗
因为 document.createTextNode
只接收一个参数作为数据,你应该像下面的代码那样改变你的 document.createTextNode
来用你的数据生成一个字符串:(在你的代码中,因为你正在使用 ,
连接字符串,实际上您将多个字符串传递给 document.createTextNode
并且它只读取第一个参数并忽略其他参数)
var mainText = document.createTextNode("You are " + ageYears + " years, " + ageMonths + "months and " + ageDays + " days old.");
或:
var mainText = document.createTextNode(`You are ${ ageYears} years, ${ageMonths} months and ${ageDays} days old.`);
这里有一个例子:
const ageYears = 30;
const ageMonths = 10;
const ageDays = 22;
function appendText() {
let p = document.createElement("p");
let mainText = document.createTextNode(`You are ${ ageYears} years, ${ageMonths} months and ${ageDays} days old.`);
p.appendChild(mainText);
document.getElementById("new-age").appendChild(p);
}
document.getElementById('appendBtn').addEventListener('click', appendText)
<div id="new-age"></div>
<button id="appendBtn">append text</button>
在 JavaScript 中,您使用 +
而不是 .
来连接字符串。
工作示例
var h1 = document.createElement("p");
h1.setAttribute("id", "mainText");
let ageYears = 20;
let ageMonths = 12
let ageDays = 24;
var mainText = document.createTextNode("You are " + ageYears + " years, " + ageMonths + " months and " + ageDays + " days old.");
h1.appendChild(mainText);
document.getElementById("new-age").appendChild(h1);
<div id="new-age"></div>
我创建了一个网站,上面会显示您的年龄。我使用 document.createTextNode 来存储输出,但输出无法正常工作。这是输出代码
var h1 = document.createElement("p");
h1.setAttribute("id", "mainText")
var mainText = document.createTextNode("You are ", ageYears, " years, ", ageMonths, "
months and ", ageDays, " days old.");
h1.appendChild(mainText);
document.getElementById("new-age").appendChild(h1);
当我 运行 我的代码时,它只输出第一部分,“你是”。有什么办法可以输出整条消息吗
因为 document.createTextNode
只接收一个参数作为数据,你应该像下面的代码那样改变你的 document.createTextNode
来用你的数据生成一个字符串:(在你的代码中,因为你正在使用 ,
连接字符串,实际上您将多个字符串传递给 document.createTextNode
并且它只读取第一个参数并忽略其他参数)
var mainText = document.createTextNode("You are " + ageYears + " years, " + ageMonths + "months and " + ageDays + " days old.");
或:
var mainText = document.createTextNode(`You are ${ ageYears} years, ${ageMonths} months and ${ageDays} days old.`);
这里有一个例子:
const ageYears = 30;
const ageMonths = 10;
const ageDays = 22;
function appendText() {
let p = document.createElement("p");
let mainText = document.createTextNode(`You are ${ ageYears} years, ${ageMonths} months and ${ageDays} days old.`);
p.appendChild(mainText);
document.getElementById("new-age").appendChild(p);
}
document.getElementById('appendBtn').addEventListener('click', appendText)
<div id="new-age"></div>
<button id="appendBtn">append text</button>
在 JavaScript 中,您使用 +
而不是 .
来连接字符串。
工作示例
var h1 = document.createElement("p");
h1.setAttribute("id", "mainText");
let ageYears = 20;
let ageMonths = 12
let ageDays = 24;
var mainText = document.createTextNode("You are " + ageYears + " years, " + ageMonths + " months and " + ageDays + " days old.");
h1.appendChild(mainText);
document.getElementById("new-age").appendChild(h1);
<div id="new-age"></div>