admin管理员组文章数量:1415684
I have searched the web but the answers showing embedding the SVG into the HTML using <object>
, <img>
, etc. What I want to do is open a large SVG file that contains lots of images, find the image I need, and show only that image on the page. How can I do that?
File structure is:
- directory
- index.html
- script.js
- images.svg
I have searched the web but the answers showing embedding the SVG into the HTML using <object>
, <img>
, etc. What I want to do is open a large SVG file that contains lots of images, find the image I need, and show only that image on the page. How can I do that?
File structure is:
- directory
- index.html
- script.js
- images.svg
-
find the image I need
how do you do this? by the way I suggest using a backend templating language. – Weilory Commented Dec 18, 2021 at 6:05 - I would guess its a sprite. – Rafael Herscovici Commented Dec 18, 2021 at 6:10
-
@Weilory It has lots of child
<g>
elements under the<svg> element, and each
g` element has alabel
attribute with the name of the image, so I can find the image I want by that name. – Damn Vegetables Commented Dec 18, 2021 at 6:50
2 Answers
Reset to default 5A few steps:
- Use the Fetch API to get the image data.
- Use a DOM parser to parse the XML into a document.
- Find the SVG subset you want. (You didn't show us your XML, but this is likely as simple as
document.querySelector()
.) - Create an SVG element. (This is actually a bit tricky due to the namespace. See also: JavaScript createElementNS and SVG)
- Append it to the relevant element on your page.
hello damn i think you can add svg in your html code and run code but you cant font-size svg in css you can use width and height in svg to big font-size ok under
本文标签: Load svg from file using JavaScriptStack Overflow
版权声明:本文标题:Load svg from file using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745239274a2649225.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论