如何访问新创建的对象的变量?
How to access newly created object's variable?
所以我有这个蓝图对象:
function User (theName, theEmail) {
this.name = theName;
this.email = theEmail;
this.quizScores = [];
this.currentScore = 0;
}
我创建了一个像这样的新用户 var user1 = new User (theName.value, theEmail.value);
当用户输入他的姓名和电子邮件时,它位于事件侦听器的函数中。现在有问题和下一个问题的按钮。每次用户单击下一个问题按钮时,我想将 currentScore 增加一个。问题是它一直保持为 0。我是这样做的:
scoretag = document.createElement("p");
scoretag.innerHTML = user1.currentScore;
body.appendChild(scoretag);
事件侦听器和主循环:
for (var i = 0; i < theChoices[question1.theChoices].length; i++) {
var arrayQ = document.createElement("p");
arrayQ.innerHTML = theChoices[question1.theChoices][i];
list.appendChild(arrayQ);
var radio = document.createElement("input");
radio.type = "radio";
listOptions.appendChild(radio);
dbtn.addEventListener("click", function() {
//list.removeChild(arrayQ);
//listOptions.removeChild
list.removeChild(list.firstChild);
list.removeChild(list.lastChild);
user1.currentScore = user1.currentScore+1;
scoretag = document.createElement("p");
scoretag.innerHTML = user1.currentScore;
body.appendChild(scoretag);
})
}
更新:我在分数递增后将用于将子项附加到 body 元素的代码放在循环中,但这会导致页面上一个接一个地打印许多数字。
但是就像我说的,当我尝试在点击按钮时递增 1 时,它仍然在屏幕上显示 0。有帮助吗?
每次增加分数,都需要更新 HTML 元素。这是一个想法:
function User (theName, theEmail) {
this.name = theName;
this.email = theEmail;
this.quizScores = [];
this.currentScore = 0;
}
var user1 = new User ("aa","bb");
function updateScore(){
user1.currentScore++;
document.getElementById('score').innerText = user1.currentScore;
}
<button id="btn" onclick="updateScore()">next</button>
<p id="score">0</p>
所以我有这个蓝图对象:
function User (theName, theEmail) {
this.name = theName;
this.email = theEmail;
this.quizScores = [];
this.currentScore = 0;
}
我创建了一个像这样的新用户 var user1 = new User (theName.value, theEmail.value);
当用户输入他的姓名和电子邮件时,它位于事件侦听器的函数中。现在有问题和下一个问题的按钮。每次用户单击下一个问题按钮时,我想将 currentScore 增加一个。问题是它一直保持为 0。我是这样做的:
scoretag = document.createElement("p");
scoretag.innerHTML = user1.currentScore;
body.appendChild(scoretag);
事件侦听器和主循环:
for (var i = 0; i < theChoices[question1.theChoices].length; i++) {
var arrayQ = document.createElement("p");
arrayQ.innerHTML = theChoices[question1.theChoices][i];
list.appendChild(arrayQ);
var radio = document.createElement("input");
radio.type = "radio";
listOptions.appendChild(radio);
dbtn.addEventListener("click", function() {
//list.removeChild(arrayQ);
//listOptions.removeChild
list.removeChild(list.firstChild);
list.removeChild(list.lastChild);
user1.currentScore = user1.currentScore+1;
scoretag = document.createElement("p");
scoretag.innerHTML = user1.currentScore;
body.appendChild(scoretag);
})
}
更新:我在分数递增后将用于将子项附加到 body 元素的代码放在循环中,但这会导致页面上一个接一个地打印许多数字。
但是就像我说的,当我尝试在点击按钮时递增 1 时,它仍然在屏幕上显示 0。有帮助吗?
每次增加分数,都需要更新 HTML 元素。这是一个想法:
function User (theName, theEmail) {
this.name = theName;
this.email = theEmail;
this.quizScores = [];
this.currentScore = 0;
}
var user1 = new User ("aa","bb");
function updateScore(){
user1.currentScore++;
document.getElementById('score').innerText = user1.currentScore;
}
<button id="btn" onclick="updateScore()">next</button>
<p id="score">0</p>