gpt4 book ai didi

javascript - For 循环覆盖 HTML 中的文本

转载 作者:搜寻专家 更新时间:2023-10-30 21:03:40 24 4
gpt4 key购买 nike

我的名为 myEmployees 的数组中有 5 个名字,但是当我运行代码时它只打印出其中的 3 个。我相信这是因为脚本中的 for 循环覆盖了它在 HTML 文档中写入的前一行。我该如何解决这个问题?

Yearly Bulletin Board Announcements!

Congratulations to Taylor, you have been here for 9 years and you meet the magic number! You get 2 extra weeks of PTO!

Derek, thank you for your service over the past 8 years. I look forward to many more years working with you!

Tyler, no longer works here.

以上是我运行代码时屏幕上显示的内容,应该还会出现另外两个语句。任何建议将不胜感激!

<body>

<h1>Yearly Buttetin Board Announcements!</h1>
<h2 id='win'></h2>
<h3 id='here'></h3>
<h4 id='notHere'></h4>

<script src='app.js'></script>
</body>

typescript

for( i = 0; i < myEmployees.length; i++ ) {
const employee = myEmployees[i];
let magicNumber = employee.extraVacay;
let stillEmployed = employee.stillEmployed;
let timeInJob = employee.timeInJob;
let name = employee.name;

if( magicNumber > 30 && timeInJob > 5 && stillEmployed == true ) {

document.getElementById('win').innerHTML = (`Congratulations to ${name}, you have been here for ${timeInJob} years and you meet the magic number! You get 2 extra weeks of PTO!`);

}
else if (stillEmployed == true) {

document.getElementById('here').innerHTML = (`${name}, thank you for your service over the past ${timeInJob} years. I look forward to many more years working with you!`);

}
else {

document.getElementById('notHere').innerHTML = (`${name}, no longer works here.`)
}
};

最佳答案

你是对的——innerHTML 正在覆盖之前的值——使用 += 代替:

document.getElementById('here').innerHTML += (`${name}, thank you for your service over the past ${timeInJob} years. I look forward to many more years working with you!`);

关于javascript - For 循环覆盖 HTML 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43083750/

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