作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图同时为两个数组创建一个循环,但执行返回“未定义”,我不明白为什么......你能看一下我的代码并向我展示方式 ?
var questions = ["a", "b", "c"];
var responses = [5, 45, "test"];
var correctionList = "";
for (var i = 0; i < questions.length; i++); {
correctionList += questions[i] + ' : ' + responses[i];
var newLi = document.createElement('li');
var liContent = document.createTextNode(correctionList);
newLi.appendChild(liContent);
console.log(correctionList);
var showCorrection = document.querySelector('#correction-list');
showCorrection.appendChild(newLi);
console.log(liContent);
}
<ol id="correction-list"></ol>
我期望一个包含“问题:回应”的列表
最佳答案
您看到的最后一个未定义
可能是因为缺少函数返回语句。仅当您在浏览器控制台上打印某些内容时才会发生这种情况。
只是稍微重构了代码,以问题:响应的格式显示问题和答案列表
function createList() {
var questions = ["a", "b", "c"];
var responses = [5, 45, "test"];
for (var i = 0; i < questions.length; i++) {
// Changed this line
var correctionList = questions[i] + ' : ' + responses[i];
var newLi = document.createElement('li');
var liContent = document.createTextNode(correctionList);
newLi.appendChild(liContent);
var showCorrection = document.querySelector('#correction-list');
showCorrection.appendChild(newLi);
}
createList();
关于javascript - 我不明白为什么我的 for 循环返回 "undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58673166/
我是一名优秀的程序员,十分优秀!