- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的代码:
var checkboxes = this.element.querySelectorAll("input[type=checkbox]") as NodeListOf<HTMLInputElement>;
checkboxes.forEach(ele => {
var key = ele.name;
if (data.hasOwnProperty(key)) {
if (!this.isArray(data[key])) {
var temp = data[key];
data[key] = [temp];
}
} else {
data[key] = [];
}
});
但是我得到一个错误:
error TS2339: Property 'forEach' does not exist on type 'NodeListOf'.
interface NodeListOf<TNode extends Node> extends NodeList {
length: number;
item(index: number): TNode;
[index: number]: TNode;
}
interface NodeList {
/**
* Returns an array of key, value pairs for every entry in the list
*/
entries(): IterableIterator<[number, Node]>;
/**
* Performs the specified action for each node in an list.
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the list.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
forEach(callbackfn: (value: Node, index: number, listObj: NodeList) => void, thisArg?: any): void;
/**
* Returns an list of keys in the list
*/
keys(): IterableIterator<number>;
/**
* Returns an list of values in the list
*/
values(): IterableIterator<Node>;
[Symbol.iterator](): IterableIterator<Node>;
}
'NodeListOf'继承自'NodeList','NodeList'有'forEach' 方法,为什么'NodeListOf'上不存在'forEach'?
最佳答案
无法保证 forEach
将存在于此类型上 - 它可以,但不一定(例如在 PhantomJS 和 IE 中),因此默认情况下 TypeScript 不允许它。为了遍历它,您可以使用:
1) Array.from():
Array.from(checkboxes).forEach((el) => { /* do something */});
2)输入:
for (let i in checkboxes) {
if (checkboxes.hasOwnProperty(i)) {
console.log(checkboxes[i]);
}
}
关于javascript - 为什么 NodeListOf 上不存在 forEach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47492595/
我的代码: var checkboxes = this.element.querySelectorAll("input[type=checkbox]") as NodeListOf;
我的代码: var checkboxes = this.element.querySelectorAll("input[type=checkbox]") as NodeListOf;
Angular 2.4.4、TypeScript 2.1.5、PhpStorm 2017.2.1我写这段代码: const list = document.querySelectorAll('path
将我的 JS 转换为 TS 严格模式。 以下语法对我来说看起来不错,但 TS 在 allSubMenus 上的 for 循环中提示: [ts] Type 'NodeListOf' is not an
let li: Element[] = document.getElementsByTagName('span'); 我收到类型转换错误,如何将值存储在 'Element[ ]' 中?? 最佳答案 从
当我尝试将 typescript 代码编译为 JavaScript 时,得到类型“NodeListOf”不是数组类型或字符串类型。 if (query.length) {
将子节点附加到li时,出现类似类型“NodeListOf”上不存在属性“appendChild”的错误 let li: NodeListOf = document.querySelectorAll("
我是一名优秀的程序员,十分优秀!