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

浏览器兼容性

另见