SVGTransformList: replaceItem() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Phương thức replaceItem() của giao diện SVGTransformList thay thế một phần tử hiện có trong danh sách bằng một phần tử mới.

Phần tử được chèn là chính phần tử đó chứ không phải bản sao.

  • Nếu newItem đã có trong một danh sách, nó sẽ bị xóa khỏi danh sách trước đó trước khi được chèn vào danh sách này.

  • Nếu phần tử đã có trong danh sách này, lưu ý rằng index của phần tử cần thay thế là trước khi xóa phần tử.

Cú pháp

js
replaceItem(newItem, index)

Tham số

newItem

Một phần tử SVGTransform được chèn vào danh sách.

index

Một integer; chỉ số tại đó phần tử mới cần thay thế phần tử hiện có, là unsigned long.

Giá trị trả về

Một đối tượng SVGTransform; phần tử được chèn vào từ danh sách.

Ngoại lệ

Phương thức này có thể ném ra DOMException với một trong các kiểu sau:

NoModificationAllowedError DOMException

Được ném ra nếu SVGTransformList tương ứng với thuộc tính chỉ đọc hoặc khi bản thân đối tượng là chỉ đọc.

IndexSizeError DOMException

Được ném ra nếu số chỉ số lớn hơn hoặc bằng numberOfItems.

Ví dụ

Thay thế một biến đổi trong danh sách

html
<svg width="200" height="200" id="mySvg">
  <rect width="100" height="100" fill="blue" transform="translate(50,50)" />
</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 rotation transformation
const rotateTransform = svgElement.createSVGTransform();
rotateTransform.setRotate(45, 50, 50);

transformList.replaceItem(rotateTransform, 0);

// Log the details of the new transformation
console.log(`New Transformation Type: ${transformList.getItem(0).type}`); // Output: 4 (e.g. SVG_TRANSFORM_ROTATE)

Thông số kỹ thuật

Thông số kỹ thuật
Scalable Vector Graphics (SVG) 2
# __svg__SVGNameList__replaceItem

Tương thích trình duyệt

Xem thêm