最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Change the fill of all elements inside an svg using javascript - Stack Overflow

programmeradmin5浏览0评论

I would like to change the fill color of all elements inside an SVG. The elements are both type rect and polygon. I have not set a default color.

Here is a sample file:

.svg

How can I do this most elegantly?

I would like to change the fill color of all elements inside an SVG. The elements are both type rect and polygon. I have not set a default color.

Here is a sample file:

http://www.kingoslo./instruments/test.svg

How can I do this most elegantly?

Share Improve this question edited Nov 5, 2012 at 1:50 alex 491k204 gold badges889 silver badges991 bronze badges asked Nov 5, 2012 at 1:47 Mikkel RevMikkel Rev 9013 gold badges14 silver badges31 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

If you have jQuery:

$('svg').children().css('fill', '#ffffff');

EDIT: Note that this assumes the rects and polygons are the direct children of the svg element.

Another variant similar to the one by Robert Longson:

document.documentElement.style.fill = 'blue';

I prefer documentElement over rootElement because it's in DOM Core, and they're mostly the same.

This worked for me while working with a plex SVG having a lot of nested elements:

$('svg').find('path, text').css('fill', '#ffffff')

发布评论

评论列表(0)

  1. 暂无评论