使用Node.attributes打破最新的打字稿版本

时间:2018-04-05 20:52:22

标签: xml typescript dom

我有一些旧的Typescript代码遍历XML文档。在不同的地方,它会读取节点及其属性,如下所示:

var attribs = node.attributes;

使用最新版本的TS(2.8.1),我将此视为众多重大变化之一。为什么要排除此属性属性,可以采用哪种解决方法?

我目前的解决方法是:

var attribs = node['attributes'];

1 个答案:

答案 0 :(得分:1)

Node类上不存在属性,它们存在于Element上。因此,您应该更改类型以使用与Element一起使用的类型(即HTMLCollection ..)

    const div = document.createElement('div') as any;

    const divA = div as Element;
    const attributes = divA.attributes; // ok

    const divB = divB as Node;
    divB.attributes; // not ok
相关问题