twh*_*mon 8 javascript foreach
我想用class删除所有元素sample.
这在Chrome和Safari中运行良好:
document.querySelectorAll('.sample').forEach(function(e) {
e.parentNode.removeChild(e);
});
Run Code Online (Sandbox Code Playgroud)
这是我在Firefox中遇到的错误:
TypeError:document.querySelectorAll(...).forEach不是函数
syn*_*t1c 12
document.querySelectorAll 返回一个NodeList,它像一个数组索引,但不是一个数组,所以你不能调用它上面的数组方法.
您可以Array.from(nodeList)在ES6或Array.prototype.slice.call(nodeList)ES5中使用
Array.from(document.querySelectorAll('selector')).forEach(el => el)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4611 次 |
| 最近记录: |