我试图通过各自功能各自的提示从用户收集信息。我测试了这些功能后,它们各自的功能都会自己工作。接下来,我试图使用函数financialInfoDisplay()在Div中显示信息,但出现以下错误消息
未捕获的TypeError:无法在以下位置设置null的属性“ innerHTML”
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
中检查undefined,在您的情况下不需要返回值。
使用代码功能financialInfoDisplay()
在加载时仅调用1次。
您不应放入标头标记,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/