如何使用<h1>标记

How to output a variable using the <h1> tag in Javascript?

本文关键字:标记 h1 何使用      更新时间:2023-09-26

我试图输出" Hello firstName lastName!在HTML的标题使用<h1>,但我不知道怎么做。现在我只是声明了变量,但如果我尝试使用<h1>输出它们,它不使用变量,只是输出为"firstName lastName"。任何帮助都是感激的!这是我现在的代码。

<html>
<head>
<title>Test</title>
</head>
<style>
body {
    background-color: #ffccff;
}
h1 {
    color: red;
    text-align: center;
}
p {
    font-family: "Arial";
    font-size: 12px;
}
</style>
<body>
</body>
<script>
var firstName = 'Ryan';
var lastName = 'Higgins';
</script>
</html>
var h1 = document.createElement("h1");                
var h1Text = document.createTextNode("Hello " + firstName + " " + lastName +" !");         
h1.appendChild(h1Text);                              
document.body.appendChild(h1);