英文:
How do I use event listeners to change the title and footer text to a prompt entered into a text box
问题
我试图将页眉和页脚文本更改为文本框中输入的任何内容。代码不能使用document.write()
我的当前脚本代码如下:
var headerchange = document.createTextNode(headerText);
h.appendChild(headerchange);
function header() {
document.getElementById("Text").innerHTML = headerText;
}
document.addEventListener("input", footer);
function footer() {
document.getElementById("Text").innerHTML = footText;
}
function enter() {
document.getElementById("done").element.click();
}
英文:
I'm trying to change the header and footer text from what its currently saying to whatever is entered into a text box. Code cannot be document.write()
my current script code is listed below
var headerchange = document.createTextNode(headerText);
h.appendchild(headerchange);
function header() {
document.getElementById("Text").innerHTML = headerText;
}
document.addeventListener("input", footer);
function footer() {
document.getElementById("Text").innerHTML = footText;
}
function enter() {
document.getElementById("done")element.click();
}
答案1
得分: 0
以下是您要翻译的代码部分:
为什么你在这里将id用作文本 `input type=text id=headerText /label br`,因为你在这里将id用作headerText
const headerTextBox = document.getElementById("headerText");
const footerTextBox = document.getElementById("footerText");
headerTextBox.addEventListener("input", updateHeader);
footerTextBox.addEventListener("input", updateFooter);
function updateHeader() {
const headerText = headerTextBox.value;
const newHeaderText = document.createTextNode(headerText);
const headerElement = document.querySelector("h1");
headerElement.innerHTML = "";
headerElement.appendChild(newHeaderText);
}
function updateFooter() {
const footerText = footerTextBox.value;
const newFooterText = document.createTextNode(footerText);
const footerElement = document.querySelector("h2");
footerElement.innerHTML = "";
footerElement.appendChild(newFooterText);
}
请注意,我已经去掉了HTML标签中的HTML实体编码(例如,"
被翻译为双引号)。
英文:
why are you using id as text input type=text id=headerText /label br
as here you are using id as headerText
const headerTextBox = document.getElementById("headerText");
const footerTextBox = document.getElementById("footerText");
headerTextBox.addEventListener("input", updateHeader);
footerTextBox.addEventListener("input", updateFooter);
function updateHeader() {
const headerText = headerTextBox.value;
const newHeaderText = document.createTextNode(headerText);
const headerElement = document.querySelector("h1");
headerElement.innerHTML = "";
headerElement.appendChild(newHeaderText);
}
function updateFooter() {
const footerText = footerTextBox.value;
const newFooterText = document.createTextNode(footerText);
const footerElement = document.querySelector("h2");
footerElement.innerHTML = "";
footerElement.appendChild(newFooterText);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论