简体   繁体   English

为什么使用getClientRects()的相同代码会产生不同的结果?

[英]Why does the same code using getClientRects() produce different results?

I have the code snippet below. 我有下面的代码片段。 If you press "Run code snippet" then you will see the following 如果按“运行代码段”,则会看到以下内容

1 - p.getClientRects().length 1 - p.getClientRects()。length

2 - span.getClientRects().length 2 - span.getClientRects()。length

But if you press "Expand snippet" at first, and then "Run code snippet" then you will see a bit different result: 但是如果你先按“扩展片段”,然后按“运行代码片段”,那么你会看到一些不同的结果:

1 - p.getClientRects().length 1 - p.getClientRects()。length

1 - span.getClientRects().length 1 - span.getClientRects()。length

I just want to understand Element.getClientRects() method. 我只想了解Element.getClientRects()方法。 But this situation crashes me. 但是这种情况让我崩溃了。 Could you explain why it produces different results? 你能解释为什么会产生不同的结果吗?

My browser is: Chrome Version 67.0.3396.99 (Official Build) (64-bit) 我的浏览器是:Chrome版本67.0.3396.99(官方版本)(64位)

 var p = document.querySelector('p'); var span = document.querySelector('span'); console.log(p.getClientRects().length, "- p.getClientRects().length"); console.log(span.getClientRects().length, "- 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> 

Un-expanded, the text wraps, so you have two separate rectangles: 未展开,文本换行,因此您有两个单独的矩形:

在此输入图像描述

Expanded, the text all fits on one line, so you have only one rectangle: 扩展后,文本全部适合一行,因此您只有一个矩形:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在不同的JS对象中使用my方法会产生相同的结果? - Why does the my method in different JS objects produce the same results? 为什么在字符串数组上使用Array.map(parseInt)会产生不同的结果 - Why does using Array.map(parseInt) on an array of strings produce different results 解析:Cloud JS代码与Swift不会产生相同的结果 - Parse : Cloud JS code vs Swift does not produce the same results 为什么.css('fontSize')会在Edge中产生不同的结果? - Why does .css('fontSize') produce different results in Edge? 为什么绝对定位会影响getClientRects() - Why does absolute positioning effect getClientRects() 为什么这段代码有两个不同的结果? - Why does this code have two different results? 为什么这些语法上相同的功能会产生不同的结果? - Why these syntactically identical functions produce different results? 为什么 async/await 然后产生不同的结果 - Why async/await and then produce different results 为什么这段代码在JavaScript中产生3? - Why does this code produce 3 in JavaScript? 为什么在React和纯CSS中重新编码相同的代码会产生不同的结果: - Why does the same code renered in React and in plain CSS provide different results:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM