作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的名为 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/
我是一名优秀的程序员,十分优秀!