gpt4 book ai didi

javascript - Element.getClientRects() 何时返回多个对象的集合?

转载 作者:太空狗 更新时间:2023-10-29 13:27:01 37 4
gpt4 key购买 nike

每次我调用 Element.getClientRects() 时,它都会返回一个仅包含一个 DOMRect 对象的集合。

Element.getClientRects() 何时返回多个 DOMRect 对象的集合?

function handleClick() {
console.log(event.target.getClientRects())
}
<ul 
style="border: 1px solid black;"
onclick="handleClick()"
>
<li>Click the text to see in console</li>
</ul>

最佳答案

The return value of Element.getClientRects() method is a collection of DOMRect objects, each associated with one CSS border-box around an element.

当元素有多个 border-boxes(如内联元素)时,Element.getClientRects() 返回多个 DOMRect 对象。一个例子:

let p = document.querySelector('p');
let span = document.querySelector('span');

console.log(p.getClientRects().length);
console.log(span.getClientRects().length);
p {
border: 1px solid green;
}
span {
border: 1px solid red;
}
<p>
This is a paragraph with
<span>Span Element having a looooooooooooooooooooooo nnnnnnnnnnnnnnnnnnnn ggggggggggggggggg text</span>
</p>

此外,返回值取决于屏幕分辨率。尺寸越小,CSS border-boxes 的数量就越少。

关于javascript - Element.getClientRects() 何时返回多个对象的集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53684908/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com