SVGTransformList: getItem() 方法

Baseline 已广泛支持

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

SVGTransformList 接口的 getItem() 方法会从列表中返回指定的项。

返回的项本身,而不是副本。对该项所做的任何更改都会立即反映在列表中。

第一项的索引为 0

语法

js
getItem(index)

参数

index

一个 integer;指定项的索引(无符号长整数)。

返回值

一个 SVGTransform 对象;列表中的指定项。

异常

NoModificationAllowedError DOMException

如果 SVGTransformList 对应于只读属性,或者对象本身是只读的,则会抛出此异常。

示例

从变换列表中访问项

html
<svg width="200" height="200" id="mySvg">
  <rect width="100" height="100" fill="blue" />
</svg>
js
const svgElement = document.querySelector("svg");
const rectElement = svgElement.querySelector("rect");

// Access the transform list of the <rect> element
const transformList = rectElement.transform.baseVal;

// Apply a translate transformation to the <rect> element
const translateTransform = svgElement.createSVGTransform();
translateTransform.setTranslate(50, 50);
transformList.appendItem(translateTransform);

// Get the first item from the transform list
const firstTransform = transformList.getItem(0);

// Log the transformation type
console.log(`Transformation Type: ${firstTransform.type}`); // Output: 2 (for SVG_TRANSFORM_TRANSLATE)

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGNameList__getItem

浏览器兼容性

另见