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
indexcủa phần tử cần thay thế là trước khi xóa phần tử.
Cú pháp
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:
NoModificationAllowedErrorDOMException-
Được ném ra nếu
SVGTransformListtương ứng với thuộc tính chỉ đọc hoặc khi bản thân đối tượng là chỉ đọc. IndexSizeErrorDOMException-
Đượ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
<svg width="200" height="200" id="mySvg">
<rect width="100" height="100" fill="blue" transform="translate(50,50)" />
</svg>
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> |