我在HTML中嵌入一个SVG对象,如下所示:
<object id='mapObject' type="image/svg+xml" data="assets/maps/drawing.svg">
</object>
Run Code Online (Sandbox Code Playgroud)
然后,我想从我的打字稿类(来自Ionic 2,但这应该是无关紧要的)达到它:
let map = document.getElementById("mapObject");
let svgDoc = map.contentDocument; // Get the inner SVG DOM
Run Code Online (Sandbox Code Playgroud)
这适用于浏览器,但是typescript抱怨它:
typescript属性'contentDocument'在类型'HTMLElement'上不存在
更糟糕的是,它在设备中不起作用,因为编译器拒绝为它生成代码.
我的临时黑客/解决方案如下:
let svgDoc = (<HTMLIFrameElement>map).contentDocument; // Get the inner SVG DOM
Run Code Online (Sandbox Code Playgroud)
这在设备中有效,因为HTMLIFrameElement具有contentDocument属性,但在我看来,typescript应该有一个特定的类型.但是我一直都找不到它.
任何人?
您可以将其转换/断言为具有contentDocument属性的HTMLObjectElement:
let map = document.getElementById("mapObject") as HTMLObjectElement;
let svgDoc = map.contentDocument; // should be fine
Run Code Online (Sandbox Code Playgroud)