我正在尝试将参数传递给 svg,但我没有做错什么,你能帮我一下吗?
链接:codesandbox
网页:
<object type="image/svg+xml" data="button.svg?color=yellow">
<param name="color" value="red" />
<param name="label" value="stop" />
</object>
Run Code Online (Sandbox Code Playgroud)
图像:
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 110 40"
width="100%"
height="100%">
<g>
<rect
id="button_rect"
x="5"
y="5"
width="100"
height="30"
rx="15"
ry="15"
fill="param(color) blue"
stroke="navy"
/>
<text id="button_label" x="55" y="30" text-anchor="middle"
font-size="25" fill="white" font-family="Verdana"
content-value="param(label)">
Ok
</text>
</g>
</svg>
Run Code Online (Sandbox Code Playgroud)
编写规范的人注意到,虽然浏览器本身没有实现这一点,但您需要包含一个 Javascript 库。他们遗漏的内容是您应该包含的地方。显然,您的第一个想法可能是将其包含在 html 文档中,但事实并非如此 - 它需要包含在 .svg 文件中,如下所示:
<script type="text/ecmascript" xlink:href="https://dev.w3.org/SVG/modules/ref/master/ref2.js"></script>
Run Code Online (Sandbox Code Playgroud)
此外,我建议使用语法引用参数url(#parameterName)。
例如:
<html>
<head>
<title></title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<object type="image/svg+xml" data="button.svg?fill=red">
<param name="fill" value="blue" />
<param name="stroke" value="red" />
</object>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
和按钮.svg:
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 110 40"
width="100%"
height="100%">
<defs>
<ref id="paramX" param="fill" default="red"/>
<ref id="paramY" param="stroke" default="blue"/>
</defs>
<g>
<rect
id="button_rect"
x="5"
y="5"
width="100"
height="30"
rx="15"
ry="15"
fill="url(#paramX)"
stroke="url(#paramY)"
/>
<text
id="button_label"
x="55"
y="30"
text-anchor="middle"
font-size="25"
fill="white"
font-family="Verdana" >
Go
</text>
</g>
<script type="text/ecmascript" xlink:href="https://dev.w3.org/SVG/modules/ref/master/ref2.js"></script>
</svg>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8136 次 |
| 最近记录: |