我有一个SVG需要在页面加载后动态修改。
有些元素使用fill="currentColor"
,另一些使用渐变填充,其中的 stop
元素使用stop-color="currentColor"
。想法是,我应该能够简单地更改父SVG的color
属性,所有子元素应自动更改颜色。
对于使用fill="currentColor"
的元素很好用,但是使用渐变填充的元素没有改变颜色。
是否有任何方法可以强制刷新?
我有一个SVG需要在页面加载后动态修改。
有些元素使用fill="currentColor"
,另一些使用渐变填充,其中的 stop
元素使用stop-color="currentColor"
。想法是,我应该能够简单地更改父SVG的color
属性,所有子元素应自动更改颜色。
对于使用fill="currentColor"
的元素很好用,但是使用渐变填充的元素没有改变颜色。
是否有任何方法可以强制刷新?
webkit
的bug!由于linearGradient
元素在defs
元素内,而且你在stop
元素上使用了currentColor
,但它并没有被继承。你也可以针对linearGradient
的子元素<stop>
元素的stop-color
属性进行操作。
https://jsfiddle.net/bb9c8gnh/10/
$("#button").on("click", function() {
$("svg").attr({color: "blue"});
$("svg linearGradient stop").eq(0).attr({"stop-color": "blue", offset:"100%"});
$("svg linearGradient stop").eq(1).attr({"stop-color": "blue"});
});