SVGTransformList:removeItem() 方法

Baseline 已廣泛支援

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

SVGTransformList 介面的 removeItem() 方法用於從列表中移除現有項。

語法

js
removeItem(index)

引數

index

一個 integer;要移除項的索引,為無符號長整型。

返回值

一個 SVGTransform 物件;從列表中移除的項。

異常

此方法可能會丟擲以下型別之一的DOMException

NoModificationAllowedError DOMException

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

IndexSizeError DOMException

如果索引號大於或等於 numberOfItems,則會丟擲此異常。

示例

從列表中移除變換

html
<svg width="200" height="200" id="mySvg">
  <rect
    width="100"
    height="100"
    fill="blue"
    transform="translate(50,50) rotate(45)" />
</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;

const removedTransform = transformList.removeItem(0);
console.dir(removedTransform); // Output: SVGTransform { type: 2, matrix: SVGMatrix, angle: 0 }

// The updated list length
console.log(`Updated number of transformations: ${transformList.length}`); // Output: 1

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGNameList__removeItem

瀏覽器相容性

另見