【发布时间】:2018-01-02 21:59:02
【问题描述】:
我无法弄清楚这里出了什么问题,我确信这很简单,但是搜索它并不容易,所以如果这个问题已经在其他地方得到了回答,我深表歉意。
情况:
我在我的 js 文件顶部声明了一个全局数组:
var question_ids = [];
数组得到一些值:
$(".quiz-game-container .row").each(function(loop_index) {
question_ids.push($(this).attr('id'));
}
现在我想显示一个计数器,它有数组的长度:
$(document).ready(function() {
// Sets the question info how many questions in total
console.log(question_ids.length);
$(".quiz-game-container .quiz-container .current-question-number").html("1/", question_ids.length);
});
问题
奇怪的是,我在浏览器开发者控制台中看到了正确的 console.log 输出(长度为 10),但它在 1 秒后消失(可能是所有内容都已加载)并且没有任何值,我在 html 视图中获取 1/。
下面是消失的 console.log 的一个活生生的例子: my website
点击刷新按钮F5 并查看控制台,console.log 输出将显示片刻然后消失。
编辑
点击刷新按钮
F5并查看控制台,console.log 输出将显示片刻然后消失。
这似乎是浏览器问题。它在 FireFox 上运行良好。
【问题讨论】:
-
缺少一些代码。数组如何填充值?
-
@Pointy 你有
question_ids.push($(this).attr('id')); -
$(".quiz-game-container .quiz-container .current-question-number").html("1/" + question_ids.length);
-
尝试使用
.text("1/" + question_ids.length) -
消失了是什么意思?您使用的是哪个浏览器?通常,当您刷新页面时,控制台会被清除。
标签: javascript jquery