SVGPointList: insertItemBefore() 方法

Baseline 已广泛支持

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

SVGPointList 接口的 insertItemBefore() 方法会在列表中另一个项之前插入一个 DOMPoint

语法

js
insertItemBefore(obj, index)

参数

obj

一个包含要插入点的坐标的 DOMPoint 对象。

index

该对象应在其之前插入的项的索引。如果传入的索引大于列表的长度,则索引将被设置为列表长度,并且该项将插入到列表最后一项之前。

返回值

已插入的 DOMPoint 对象。

异常

NoModificationAllowedError DOMException

如果列表是只读的,则会抛出此错误。

示例

以下示例显示了一个包含五个坐标对的 <polyline> 的 SVG。创建一个新的 DOMPoint,并将其插入到索引为 2 的点之前。

html
<svg id="svg" viewBox="-10 -10 120 120" xmlns="http://www.w3.org/2000/svg">
  <polyline
    id="example"
    stroke="black"
    fill="none"
    points="50,0 21,90 98,35 2,35 79,90" />
</svg>
js
const example = document.getElementById("example");
const point = document.getElementById("svg").createSVGPoint();
point.y = 10;
point.x = 10;
console.log(example.points.insertItemBefore(point, 2));

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGNameList__insertItemBefore

浏览器兼容性