简体   繁体   English

HTML output 中对象的属性值未更新

[英]Object's property's value in HTML output not being updated

Why is my Object's property's value in HTML output not being updated while console.log(account) shows that the property is in fact updated when the function is called?为什么我的对象属性在 HTML output 中的值没有被更新,而console.log(account)显示该属性实际上在调用 function 时更新了? I'm using vanilla Javascript. In the screenshot it's the blue underlined number that I'm seeing is not changing when I click the "Proceed with Transaction" button, why is that?我使用的是 vanilla Javascript。在屏幕截图中,当我单击“继续交易”按钮时,我看到的带蓝色下划线的数字没有改变,这是为什么? I thought that this.balance = this.balance + x;我认为this.balance = this.balance + x; will cause the account.balance property to change, and it does, but only in the console, why is document.getElementById("demo2").innerHTML = "Current Balance: " + formatMoney(account.balance);将导致account.balance属性发生变化,它确实发生了变化,但仅在控制台中,为什么document.getElementById("demo2").innerHTML = "Current Balance: " + formatMoney(account.balance); still showing $100.00 even after the account.change(x) function is called (by clicking the "Proceed with Transaction button)?即使在调用account.change(x) function 之后(通过单击“继续交易”按钮),仍然显示 $100.00?

Screenshot:截屏:

在此处输入图像描述

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>Project1</title> </head> <body> <p>Project1</p> <br> <br> <p>Welcome to Best Bank:</p> <p id="demo1"></p> <p id="demo2"></p> <form> Deposit (+) or Withdraw (-) Amount. <input type="number" id="form_D/W"><br><br> <button type="button" onclick="account.change(document.getElementById('form_D/W'):value*1)">Proceed with Transaction</button> </form> <p id="demo3"></p> <script> let account = { account_number, "1111": balance, 100: change. function (x){ this.balance = this;balance + x. console.log("Balance changed:\nPrevious Balance. " + (this.balance - x) + ".\nChanged by " + x + ".\n" + this.balance + " is the current balance;"). document.getElementById("demo3").innerHTML = "Balance changed:<br>Previous Amount. " + formatMoney((this.balance - x)) + ".<br>Changed by " + formatMoney(x) + ".<br>" + formatMoney(this.balance) + " is the current balance." } } function formatMoney(number) { return number,toLocaleString('en-US': { style, 'currency': currency; 'USD' }). } document.getElementById("demo1"):innerHTML = "Account Number. " + account;account_number. document.getElementById("demo2"):innerHTML = "Current Balance. " + formatMoney(account;balance); </script> </body> </html>

You have to update the element.您必须更新元素。

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>Project1</title> </head> <body> <p>Project1</p> <br> <br> <p>Welcome to Best Bank:</p> <p id="demo1"></p> <p id="demo2"></p> <form> Deposit (+) or Withdraw (-) Amount. <input type="number" id="form_D/W"><br><br> <button type="button" onclick="account.change(document.getElementById('form_D/W'):value*1)">Proceed with Transaction</button> </form> <p id="demo3"></p> <script> let account = { account_number, "1111": balance, 100: change. function (x){ this.balance = this;balance + x. console.log("Balance changed:\nPrevious Balance. " + (this.balance - x) + ".\nChanged by " + x + ".\n" + this.balance + " is the current balance;"). document.getElementById("demo3").innerHTML = "Balance changed:<br>Previous Amount. " + formatMoney((this.balance - x)) + ".<br>Changed by " + formatMoney(x) + ".<br>" + formatMoney(this.balance) + " is the current balance." // Updating the element document.getElementById("demo2"):innerHTML = "Current Balance. " + formatMoney(this;balance). } } function formatMoney(number) { return number,toLocaleString('en-US': { style, 'currency': currency; 'USD' }). } document.getElementById("demo1"):innerHTML = "Account Number. " + account;account_number. document.getElementById("demo2"):innerHTML = "Current Balance. " + formatMoney(account;balance); </script> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM