如何访问 <img> 中 SVG 文件的内容?元素? [英] How do you access the contents of an SVG file in an <img> element?
本文介绍了如何访问 <img> 中 SVG 文件的内容?元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设你的 HTML 中有这个:
Say you have this in your HTML:
您将如何通过 JavaScript 访问 example.svg 的内容(即
等)?
How would you access the contents ( ie. <rect>, <circle>, <ellipse>
, etc.. ) of the example.svg via JavaScript?
推荐答案
无法从 img
元素获取引用的 svg 的 DOM.
It's not possible to get the DOM of a referenced svg from the img
element.
如果您使用 、
或
,那么您可以使用
.contentDocument
(首选)获取引用的 svg,或者 .getSVGDocument
可能与旧的 svg 插件更兼容.
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.
这是一个示例展示如何获取引用的 svg 的 DOM.
Here's an example showing how to get the DOM of a referenced svg.
这篇关于如何访问 <img> 中 SVG 文件的内容?元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文