控制台日志颜色含义

Console Log Color Meaning

我很好奇在控制台日志中显示黑色或蓝色的数字之间有什么区别?

我目前正在使用这个变量输出:

currentImageIndex = currentImage.attr('data-index'),

它确实在控制台中输出了正确的数字。它以黑色显示数字。出于某种原因,即使输出了正确的数字,我的函数也不起作用。但是,如果我写以下内容:

currentImageIndex = 5, 

该功能将起作用。我注意到该数字在控制台日志中显示为蓝色。有什么不同?

黑色表示字符串,蓝色表示数字:

元素属性总是字符串; .attr returns 一个字符串。您需要将其转换为数字:

currentImageIndex = Number(currentImage.attr('data-index'));

那是因为您的 data-index 属性的值是字符串,而不是数字(数字在控制台中显示为蓝色)。

您应该使用 window.parseInt 将字符串解析为数字来将属性值解析为数字:

...
const currentImageIndexValue = currentImage.attr('data-index');
const currentImageIndex = window.parseInt(currentImageIndexValue);

只是要注意,如果属性的值不能解析为数字,currentImageIndex的值将是NaN

JavaScript 类型的一些详细信息:MDN JavaScript types