SVGUseElement: href 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

SVGUseElement 介面的只讀屬性 href 反映了給定元素的 hrefxlink:href 已棄用 屬性。

一個 SVGAnimatedString 物件。

示例

給定以下 SVG

html
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <circle id="circle1" cx="50" cy="50" r="40" fill="blue" />
    <circle id="circle2" cx="50" cy="50" r="40" fill="red" />
  </defs>
  <use href="#circle1" x="50" y="50" />
  <use href="#circle2" x="100" y="50" />
</svg>

我們可以訪問 <use> 元素的 href 屬性

js
const uses = document.querySelectorAll("use");
console.log(uses[0].href.baseVal); // output: "#circle1"
console.log(uses[1].href.baseVal); // output: "#circle2"

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGURIReference__href

瀏覽器相容性