Range:getClientRects() 方法
Range.getClientRects() 方法返回一個 DOMRect 物件列表,表示 range 在螢幕上佔用的區域。這是透過聚合範圍中所有元素的 Element.getClientRects() 呼叫結果而建立的。
語法
js
getClientRects()
引數
無。
返回值
示例
記錄選定的客戶端矩形大小
HTML
html
<div></div>
<pre id="output"></pre>
CSS
css
div {
height: 80px;
width: 200px;
background-color: blue;
}
JavaScript
js
const range = document.createRange();
range.selectNode(document.querySelector("div"));
rectList = range.getClientRects();
const output = document.querySelector("#output");
for (const rect of rectList) {
output.textContent = `${output.textContent}\n${rect.width}:${rect.height}`;
}
結果
規範
| 規範 |
|---|
| CSSOM 檢視模組 # dom-range-getclientrects |
瀏覽器相容性
載入中…