如何访问< img>中的SVG文件的内容?元件? [英] How do you access the contents of an SVG file in an <img> element?

查看:157
本文介绍了如何访问< img>中的SVG文件的内容?元件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



< img src ='example.svg'/> < rect> ;,< circle>,< ellipse> 等等..)的example.svg通过JavaScript?

解决方案不可能从 img < object> < embed>元素。 或< iframe> 然而,您可以使用 .contentDocument (首选)引用的svg或 .getSVGDocument ,它们可能与旧的svg插件更兼容。



这里有一个示例,显示如何获取DOM引用svg。


Say you have this in your HTML:

<img src='example.svg' />

How would you access the contents ( ie. <rect>, <circle>, <ellipse>, etc.. ) of the example.svg via JavaScript?

解决方案

It's not possible to get the DOM of a referenced svg from the img element.

If you use <object>, <embed> or <iframe> however then you can use .contentDocument (preferred) to get the referenced svg, or .getSVGDocument which may be more compatible with old svg plugins.

Here's an example showing how to get the DOM of a referenced svg.

这篇关于如何访问&lt; img&gt;中的SVG文件的内容?元件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆