我在类型脚本中创建一个iFrame元素:
var iFrameElement : HTMLIFrameElement = document.createElement("iframe");
iFrameElement.sandbox.add('allow-forms');
iFrameElement.sandbox.add('allow-scripts');
iFrameElement.sandbox.add('allow-same-origin');
但是,sandbox属性未定义,因此add(value:string)此处失败。
我无法弄清楚如何实例化沙盒属性,这里是lib.d.ts中定义的接口:
interface HTMLIFrameElement {
sandbox: DOMSettableTokenList;
}
interface DOMSettableTokenList extends DOMTokenList {
value: string;
}
interface DOMTokenList {
length: number;
contains(token: string): boolean;
remove(token: string): void;
toggle(token: string): boolean;
add(token: string): void;
item(index: number): string;
[index: number]: string;
toString(): string;
}
我认为只有一些语法我不知道如何在这里使用,或者还有其他一些技巧可以让它发挥作用。我在这个网站和互联网上广泛搜索,但结果并不是特别有帮助。
遗憾的是,答案here在TypeScript中不起作用(Type' String'缺少属性'值'来自类型' DOMSettableTokenList')我想把一些对象转换为该类型。我看到this guy似乎重新实现了这种类型,但现在这对我来说似乎太过苛刻了。
帮助我堆栈溢出,你是我唯一的希望。
答案 0 :(得分:2)
通常情况下,如果您正在计算某些特定于浏览器的实现,我建议您将(更改类型)转换为any
,这样您甚至可以编写:
(<any>iFrameElement).foo = "";
...但是要在代码中加入一些内容。
这将允许您将其用于IE:
iFrameElement.sandbox.add('allow-forms');
iFrameElement.sandbox.add('allow-scripts');
这适用于Chrome:
(<any>iFrameElement).sandbox = "allow-forms allow-scripts";