MediaList: phương thức item()

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 item() của giao diện MediaList trả về media query tại index được chỉ định, hoặc null nếu index được chỉ định không tồn tại.

Cú pháp

js
item(index)
[index]

Note: Có thể sử dụng cú pháp dấu ngoặc ([]) thay cho cú pháp item().

Tham số

index

Một số nguyên.

Giá trị trả về

Nếu cú pháp dấu ngoặc ([]) được sử dụng và không có mục nào cho chỉ mục đã cho, undefined được trả về.

Ví dụ

Ví dụ sau sẽ ghi vào console từng media query được lưu trong MediaList liên quan đến stylesheet đầu tiên được áp dụng cho tài liệu hiện tại.

js
const stylesheet = document.styleSheets[0];
console.log(stylesheet.media.length);
console.log(stylesheet.media.item(0)); // Returns a string like "print"
console.log(stylesheet.media.item(5)); // Returns null if there is no 5th entry
console.log(stylesheet.media[1]); // Returns a string like "print"
console.log(stylesheet.media[5]); // Returns undefined if there is no 5th entry

Thông số kỹ thuật

Thông số kỹ thuật
CSS Object Model (CSSOM)
# dom-medialist-item

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