我一直在研究一些使用ES6的JavaScript教程。到目前为止,其中两个课程一直在抛出同样的错误,并且对JavaScript不熟悉,我仍然试图理解逻辑,所以不擅长调试。我曾尝试使用Babel将ES6代码转换为普通JavaScript,认为这是一个浏览器问题,但同样的错误发生。Javascript ES6“不是功能”错误
任何帮助将不胜感激。
的ES6的JavaScript
const inputs = document.querySelectorAll('.controls input');
function handleUpdate() {
const suffix = this.dataset.sizing || '';
document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix);
}
inputs.forEach(input => input.addEventListener('change', handleUpdate));
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));
的 “巴别塔” 编译的JavaScript
var inputs = document.querySelectorAll('.controls input');
function handleUpdate() {
var suffix = this.dataset.sizing || '';
document.documentElement.style.setProperty('--' + this.name, this.value + suffix);
}
inputs.forEach(function (input) {
return input.addEventListener('change', handleUpdate);
});
inputs.forEach(function (input) {
return input.addEventListener('mousemove', handleUpdate);
});
的错误
inputs.forEach is not a function
'document.querySelectorAll'不返回数组。它返回类似数组的DOM集合,它没有'forEach'.You可以使用'Array.prototype.forEach.call(inputs,function(input){...});'。 –
谢谢Yeldar Kurmangaliyev这工作完美 –
@YeldarKurmangaliyev请参阅[forEach方法Node.childNodes?](http://stackoverflow.com/questions/36108712/foreach-method-of-node-childnodes) – guest271314