如何在Typescript中使用SVGDocument? [英] How to use SVGDocument in Typescript?
问题描述
根据W3C建议,方法 getSVGDocument()
应该返回 SVGDocument
,但lib中的代码.d.ts是:
According to the W3C recommendations, the method getSVGDocument()
should return an SVGDocument
, but the code in lib.d.ts is:
interface GetSVGDocument {
getSVGDocument(): Document;
}
那么我该怎么做?
var svgDoc:SVGDocument= document.getElementById('myId').getSVGDocument();
var svgElement:SVGElement = svgDoc.getElementById("myElement");
... further manipulations on svgElement
的进一步操作通常,它们是否由SVG实现传递
More generally, do they passed by the SVG implementation in TypeScript or am I missing something?
是否有适当的SVG DOM实现,或者我该如何实现?
Is there any proper implementation of the SVG DOM, or how could I implement it?
推荐答案
打字稿 Document
类型与 SVGDocument
类似,因此只使用 Document
应该没问题。您将需要确保将元素强制转换为正确的类型( SVGElement
或 SVGSVGElement
)。
The typescript Document
type is comparable to an SVGDocument
, so you should be fine to just use Document
. You will need to ensure that your element is casted as the right type (SVGElement
or SVGSVGElement
).
此外,您还可以在Typescript中进行双重转换。这可能允许您提出一种将元素/文档转换为 any
的方法,并在遇到更多问题时返回所需的文档/元素类型
Also, you can do double casting in Typescript. This may allow you to come up with a way to cast an element/document as any
and back to the document/element type you need in the case that you run into further issues.
这篇关于如何在Typescript中使用SVGDocument?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!