var ele = document.getElementById('element'); console.log(ele.style.width); // 空字符串 console.log(ele.style.height); // '100px'
MDN资料javascript
var ele = document.getElementById('element'); console.log(window.getComputedStyle(ele).width); // '100px' console.log(window.getComputedStyle(ele).height); // '100px'
功能与第二点相同,只存在于旧版本IE中(IE9如下),除了作旧版IE兼容,就不要用它了。java
MDN资料code
var ele = document.getElementById('element'); console.log(ele.getBoundingClientRect().width); // 100 console.log(ele.getBoundingClientRect().height); // 100
1.取整ip
1. // 丢弃小数部分,保留整数部分 2. parseInt(5/2) // 2
2.向上取整element
1. // 向上取整,有小数就整数部分加1 2. Math.ceil(5/2) // 3
3.向下取整字符串
1. // 向下取整,丢弃小数部分 2. Math.floor(5/2) // 2
4四舍五入get
1. // 四舍五入 2. Math.round(5/2) // 3
1. // 取余 2. 6%4 // 2