获取浮动盒子的高度可以通过以下几种方法实现:
1. 使用 JavaScript:
通过 `offsetHeight` 属性:`var height = box.offsetHeight;`
通过 `getBoundingClientRect` 方法:`var height = box.getBoundingClientRect().height;`
2. 使用 CSS:
可以通过为浮动盒子设置 `display: inline-block;`,然后使用 `height` 属性来指定高度。
或者使用 `::after` 伪元素来为浮动盒子创建一个高度。
下面是具体的实现代码:
使用 JavaScript 获取高度
```javascript
// 假设 box 是一个 DOM 元素
var box = document.getElementById('box');
// 方法一:使用 offsetHeight
var height1 = box.offsetHeight;
// 方法二:使用 getBoundingClientRect
var height2 = box.getBoundingClientRect().height;
console.log('Height by offsetHeight:', height1);
console.log('Height by getBoundingClientRect:', height2);
```
使用 CSS 设置高度
```css
box {
float: left;
display: inline-block; / 设置为 inline-block 可以使用 height 属性 /
height: 100px; / 设置一个具体的高度 /