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

瀏覽器相容性

另見