<script>
SVG 的script元素允許向 SVG 文件新增指令碼。
示例
html
Click the circle to change colors.
<svg
viewBox="0 0 10 10"
height="120px"
width="120px"
xmlns="http://www.w3.org/2000/svg">
<circle cx="5" cy="5" r="4" />
<script>
// <![CDATA[
function getColor() {
const R = Math.round(Math.random() * 255)
.toString(16)
.padStart(2, "0");
const G = Math.round(Math.random() * 255)
.toString(16)
.padStart(2, "0");
const B = Math.round(Math.random() * 255)
.toString(16)
.padStart(2, "0");
return `#${R}${G}${B}`;
}
document.querySelector("circle").addEventListener("click", (e) => {
e.target.style.fill = getColor();
});
// ]]>
</script>
</svg>
屬性
使用上下文
| 類別 | 無 |
|---|---|
| 允許的內容 | 任何元素或字元資料 |
規範
| 規範 |
|---|
| 可縮放向量圖形 (SVG) 2 # ScriptElement |
瀏覽器相容性
BCD 表格僅在瀏覽器中載入