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

瀏覽器相容性