现代浏览器可以通过使用SVG <use>
元素来实现此功能,而无需使用JS。但是,对于旧版浏览器(包括IE11),我建议使用svgxuse之类的JS填充程序。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
img {
border: 2px solid black;
}
svg {
border: 2px solid goldenrod;
}
svg[class*=fill-] {
font-size:56px;
font-weight: bold;
font-family: Verdana,Helvetica,sans-serif;
/* When using the <use> element,you need to set the styles in the page,but when loading it as a regular image,styles set within the SVG file will be used.
Be careful about using inline styles to set any colors,since those will override others. */
}
svg.fill-blue {
color: blue;
fill: currentColor;
}
svg.fill-green {
color: green;
fill: currentColor;
}
svg.fill-orange {
fill: orange; /* the currentColor value trick is optional */
}
</style>
</head>
<body>
<img src="external-svg-example.svg" alt="">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="720" height="90">
<image xlink:href="external-svg-example.svg" width="720" height="90"/>
</svg>
<!-- It may be desirable to place the xmlns:xlink attribute in the <html> tag instead of each <svg> tag. -->
<!-- Note that IE11 and older and some older versions of other browsers
do not support loading external SVGs via the <use> element.
JS workarounds do exist,e.g. https://github.com/Keyamoon/svgxuse
More info:
https://stackoverflow.com/questions/22516712/svg-use-tag-with-external-reference-in-ie-11 -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="720" height="90" class="fill-blue">
<use xlink:href="external-svg-example.svg#g1"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="720" height="90" class="fill-green">
<use xlink:href="external-svg-example.svg#g1"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="720" height="90" class="fill-orange">
<use xlink:href="external-svg-example.svg#g1"/>
</svg>
<script src="svgxuse.js" defer></script>
</body>
</html>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 720 90" width="720" height="90">
<style>
svg:root {
/* "svg:root" is used so this rule won't interfere with other styles when this SVG is injected into a page via JS. */
color: red;
fill: currentColor;
font-size:56px;
font-weight: bold;
font-family: Verdana,sans-serif;
}
</style>
<g id="g1">
<text y="70" x="28">This is Our Test Text</text>
</g>
</svg>
本文链接:https://www.f2er.com/3121346.html