SVGTransformList: appendItem() 方法

Baseline 已廣泛支援

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

SVGTransformList 介面的 appendItem() 方法會在列表的末尾插入一個新項。

插入的項是該項本身,而不是副本。

  • 如果 newItem 已經存在於某個列表中,在將其插入到此列表之前,會先將其從之前的列表中移除。

語法

js
appendItem(newItem)

引數

newItem

要新增到列表的 SVGTransform 項。

返回值

一個 SVGTransform 物件;列表中的已新增項。

異常

NoModificationAllowedError DOMException

如果 SVGTransformList 對應於只讀屬性,或者物件本身是隻讀的,則會丟擲此異常。

示例

新增新變換

html
<svg width="200" height="200">
  <rect width="100" height="100" fill="red" />
</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;

// Create a new translation transformation
const svgTransform = svgElement.createSVGTransform();
svgTransform.setTranslate(50, 50);

// Append the new transformation to the list
const appendedTransform = transformList.appendItem(svgTransform);

console.dir(appendedTransform); // Output: SVGTransform { type: 2, matrix: SVGMatrix, angle: 0 }
console.log(`Total number of transformations: ${transformList.numberOfItems}`); // Output: 1

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGNameList__appendItem

瀏覽器相容性

另見