SVGElement: viewportElement 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

SVGElement 接口的 viewportElement 属性表示建立了当前视口(viewport)的 SVGElement。通常是最近的祖先 <svg> 元素。如果给定的元素是最外层的 <svg> 元素,则为 null

一个 SVGElement

示例

检索 viewportElement

html
<svg id="outerSvg" width="200" height="200" xmlns="http://www.w3.org/2000/svg">
  <svg id="innerSvg" x="10" y="10" width="100" height="100">
    <circle id="circle" cx="50" cy="50" r="40" fill="blue"></circle>
  </svg>
</svg>
js
const circle = document.getElementById("circle");
const innerSvg = document.getElementById("innerSvg");
const outerSvg = document.getElementById("outerSvg");

console.log(circle.viewportElement); // Output: <svg id="innerSvg">...</svg>
console.log(innerSvg.viewportElement); // Output: <svg id="outerSvg">...</svg>
console.log(outerSvg.viewportElement); // Output: null

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGElement__viewportElement

浏览器兼容性

另见