body { background-image:
url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHdpZHRoPScxMCcgaGVpZ2h0PScxMCc+PGxpbmVhckdyYWRpZW50IGlkPSdncmFkaWVudCc+PHN0b3Agb2Zmc2V0PScxMCUnIHN0b3AtY29sb3I9JyNGMDAnLz48c3RvcCBvZmZzZXQ9JzkwJScgc3RvcC1jb2xvcj0nI2ZjYycvPiA8L2xpbmVhckdyYWRpZW50PjxyZWN0IGZpbGw9J3VybCgjZ3JhZGllbnQpJyB4PScwJyB5PScwJyB3aWR0aD0nMTAwJScgaGVpZ2h0PScxMDAlJy8+PC9zdmc+");
URL ENCODE YOUR SVG
正如其他人建议的那样,您需要对整个SVG字符串进行URL编码,以便在IE11中工作。在我的例子中,我省略了诸如fill="#00FF00"和stroke="#FF0000"等字段中的颜色值,并将它们替换为SASS变量fill="#{$color-rgb}",以便它们可以替换为我想要的颜色。你可以使用任何在线转换器对字符串的其余部分进行URL编码。您将得到如下的SVG字符串:
% 3 csvg % 20 xmlns % 3 d % 27 http % 3 a % 2 f % 2 fwww.w3.org % 2 f2000 % 2 fsvg % 20 viewbox % 3 d % 270% 27% 200% 20494.572% 20494.572% 27% 20宽度% 3 d % 27512% 27% 20高% 3 d % 27512% 27% 3 e % 0 a % 20% 20% 3 cpath % 20 d % d % 27 m257.063 % 200 c127.136 % 200% 2021.808% 20105.33% 2021.808% 20235.266 c0 % 2041.012% 2010.535% 2079.541% 2028.973% 20113.104 l3.825 % 20464.586 c345 % 2012.797% 2041.813% 2012.797% 2015.467% 200% 2029.872 - -4.721% 2041.813 -12.797 v158.184z % 20填补27% % 3 d % 27 # <强>{$色rgb} < /强> % 27% 2 f % 3 e % 3 c % 2 fsvg % 3 e < / p >
当创建你的数据URL时,你需要省略字符集,以便在IE11中工作。
不 background-image: url(data:image/svg+xml;utf-8,%3Csvg%2....) BUT background-image: url(data:image/svg+xml,%3Csvg%2....)
var container = document.querySelector(".container");
var svg = document.querySelector("svg");
var svgText = (new XMLSerializer()).serializeToString(svg);
container.style.backgroundImage = `url(data:image/svg+xml;utf8,${encodeURIComponent(svgText)})`;