StylePropertyMapReadOnly: phương thức forEach()
Khả dụng hạn chế
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Phương thức StylePropertyMapReadOnly.forEach() thực thi một hàm được cung cấp một lần cho mỗi phần tử của StylePropertyMapReadOnly.
Cú pháp
js
forEach(callbackFn)
forEach(callbackFn, thisArg)
Tham số
callbackFn-
Hàm được thực thi cho mỗi phần tử, nhận ba đối số:
currentValue-
Giá trị của phần tử hiện tại đang được xử lý.
indexOptional-
Chỉ mục của phần tử hiện tại đang được xử lý.
arrayOptional-
StylePropertyMapReadOnly mà
forEach()đang được gọi trên đó.
thisArgOptional-
Giá trị dùng làm
this(tức là đối tượng tham chiếuObject) khi thực thicallback.
Giá trị trả về
Không có (undefined).
Ví dụ
Đây là ví dụ sử dụng forEach() trên Element.computedStyleMap() đã được lấy về.
js
// get a button element
const buttonEl = document.querySelector(".example");
// we can retrieve all computed styles with `computedStyleMap`
const allComputedStyles = buttonEl.computedStyleMap();
// forEach will allow us to run code over each prop/val pair
allComputedStyles.forEach((elem, index, arr) => {
// code to run for each pair
});