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?
From IE9 and above you can use SVG in a ordinary IMG tag..
SVG images can be written directly into the HTML document using the <svg> </svg> tag. To do this, open the SVG image in VS code or your preferred IDE, copy the code, and paste it inside the <body> element in your HTML document.
SVG is, essentially, to graphics what HTML is to text. SVG images and their related behaviors are defined in XML text files, which means they can be searched, indexed, scripted, and compressed. Additionally, this means they can be created and edited with any text editor or with drawing software.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With