gpt4 book ai didi

javascript - 如何插入由几个不同函数中的提示收集的信息以显示在 JavaScript 的 Div 元素中?

转载 作者:行者123 更新时间:2023-12-01 01:04:40 26 4
gpt4 key购买 nike

我试图通过各自功能的提示从用户那里收集信息。正如我所测试的,这些自己的功能中的每一个都是独立工作的。接下来,我尝试使用函数 FinancialInfoDisplay() 在 Div 中显示信息,但收到以下错误消息

Uncaught TypeError: Cannot set property 'innerHTML' of null at financialInfoDisplay

并且提示未在浏览器中显示。这是为什么?我该如何解决它?

我什至尝试过在每个函数中添加将 .innerHTML 添加到 div 的代码,这有效,但是一旦我向 div 添加另一个提示,第一个提示就会消失。

我什至尝试添加参数而不是全局变量,例如 var userWithdrawal、userName、depositAmount 到 FinancialInfoDisplay() 中,然后在调用所述函数时将这些变量作为参数传递,但这也不起作用。

//GLOBAL VARIABLES
var userWithdrawal, userName, depositAmount;

//GETS ELEMENT FROM DOM
var $ = function (id) {
"use strict";
return window.document.getElementById(id);
};
//GETS USER NAME
function namePrompt () {
"use strict";
userName = window.prompt("Please enter your name");
return userName;
}

//GETS DEPOSIT AMOUNT
function depositAmountPrompt () {
"use strict";
depositAmount = window.prompt("Please enter the amount of money you would like to deposit");
return depositAmount;

}
//GETS WITHDRAWAL Amount
function withdrawalAmount () {
"use strict";
userWithdrawal = window.prompt("Please enter the amount of money you would like to withdrawal from your account");
return userWithdrawal;
}

//DISPLAYS THE PROMPTS WITHIN A DIV
function financialInfoDisplay() {
"use strict";
var infoDisplay = window.document.getElementById('infoDisplay').innerHTML = userName + depositAmount + userWithdrawal;
return infoDisplay;
}

financialInfoDisplay();

//HANDLES ALL EVENT LISTENERS
function init() {
"use strict";
$('nameBtn').addEventListener("click", namePrompt);
$('depositBtn').addEventListener("click", depositAmountPrompt);
$('withdrawlBtn').addEventListener("click", withdrawalAmount)

}
window.addEventListener("load", init);

<style>body {
width: 500px;
margin: 0 auto;
}

#infoDisplay {
border: 3px solid black;
height: 250px;
}

</style>
<body>
<button type="button" id="nameBtn" value>Name</button>
<button type="button" id="depositBtn">Deposit</button>
<button type="button" id="withdrawlBtn">Withdrawl</button>
<div id="infoDisplay"></div>
</body>

最佳答案

您应该在每个点击事件中调用financialInfoDisplay,并检查financialInfoDisplay中的未定义,并且在您的情况下不需要返回值。

使用您的代码函数 financialInfoDisplay() 在加载时仅调用 1 次。

您不应该放入 header 标签,nameBtn 未生成,无法为其设置事件处理程序。

HTML 内容:

<style>
body {
width: 500px;
margin: 0 auto;
}

#infoDisplay {
border: 3px solid black;
height: 250px;
}
</style>

<body>
<button type="button" id="nameBtn" value>Name</button>
<button type="button" id="depositBtn">Deposit</button>
<button type="button" id="withdrawlBtn">Withdrawl</button>
<div id="infoDisplay"></div>
<script>
//GLOBAL VARIABLES
var userWithdrawal, userName, depositAmount;

//GETS ELEMENT FROM DOM
var $ = function (id) {
"use strict";
return window.document.getElementById(id);
};
//GETS USER NAME
function namePrompt() {
"use strict";
userName = window.prompt("Please enter your name");
financialInfoDisplay();
//return userName;
}

//GETS DEPOSIT AMOUNT
function depositAmountPrompt() {
"use strict";
depositAmount = window.prompt("Please enter the amount of money you would like to deposit");
//return depositAmount;
financialInfoDisplay();

}
//GETS WITHDRAWAL Amount
function withdrawalAmount() {
"use strict";
userWithdrawal = window.prompt("Please enter the amount of money you would like to withdrawal from your account");
financialInfoDisplay();
//return userWithdrawal;
}

//DISPLAYS THE PROMPTS WITHIN A DIV
function financialInfoDisplay() {
"use strict";
if (userName != undefined) {
var infoDisplay = window.document.getElementById('infoDisplay').innerHTML = userName;
}
if (depositAmount != undefined) {
var infoDisplay = window.document.getElementById('infoDisplay').innerHTML += depositAmount;
}
if (userWithdrawal != undefined) {
var infoDisplay = window.document.getElementById('infoDisplay').innerHTML += userWithdrawal;
}

//return infoDisplay;
}

//HANDLES ALL EVENT LISTENERS
function init() {
"use strict";
$('nameBtn').addEventListener("click", namePrompt);
$('depositBtn').addEventListener("click", depositAmountPrompt);
$('withdrawlBtn').addEventListener("click", withdrawalAmount)

}
window.addEventListener("load", init);
</script>
</body>

function financialInfoDisplay() {
"use strict";
if(userName != undefined){
var infoDisplay = window.document.getElementById('infoDisplay').innerHTML = userName;
}
if(depositAmount != undefined){
var infoDisplay = window.document.getElementById('infoDisplay').innerHTML += depositAmount;
}
if(userWithdrawal != undefined){
var infoDisplay = window.document.getElementById('infoDisplay').innerHTML += userWithdrawal;
}

//return infoDisplay;
}

//GLOBAL VARIABLES
var userWithdrawal, userName, depositAmount;

//GETS ELEMENT FROM DOM
var $ = function (id) {
"use strict";
return window.document.getElementById(id);
};
//GETS USER NAME
function namePrompt () {
"use strict";
userName = window.prompt("Please enter your name");
financialInfoDisplay();
//return userName;
}

//GETS DEPOSIT AMOUNT
function depositAmountPrompt () {
"use strict";
depositAmount = window.prompt("Please enter the amount of money you would like to deposit");
//return depositAmount;
financialInfoDisplay();

}
//GETS WITHDRAWAL Amount
function withdrawalAmount () {
"use strict";
userWithdrawal = window.prompt("Please enter the amount of money you would like to withdrawal from your account");
financialInfoDisplay();
//return userWithdrawal;
}

//DISPLAYS THE PROMPTS WITHIN A DIV
function financialInfoDisplay() {
"use strict";
if(userName != undefined){
var infoDisplay = window.document.getElementById('infoDisplay').innerHTML = userName;
}
if(depositAmount != undefined){
var infoDisplay = window.document.getElementById('infoDisplay').innerHTML += depositAmount;
}
if(userWithdrawal != undefined){
var infoDisplay = window.document.getElementById('infoDisplay').innerHTML += userWithdrawal;
}

//return infoDisplay;
}



//HANDLES ALL EVENT LISTENERS
function init() {
"use strict";
$('nameBtn').addEventListener("click", namePrompt);
$('depositBtn').addEventListener("click", depositAmountPrompt);
$('withdrawlBtn').addEventListener("click", withdrawalAmount)

}
window.addEventListener("load", init);
<style>body {
width: 500px;
margin: 0 auto;
}

#infoDisplay {
border: 3px solid black;
height: 250px;
}

</style>
<body>
<button type="button" id="nameBtn" value>Name</button>
<button type="button" id="depositBtn">Deposit</button>
<button type="button" id="withdrawlBtn">Withdrawl</button>
<div id="infoDisplay"></div>
</body>

关于javascript - 如何插入由几个不同函数中的提示收集的信息以显示在 JavaScript 的 Div 元素中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55756675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com