本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议,欢迎转载、或重新修改使用,但需要注明来源。 [署名 4.0 国际 (CC BY 4.0)](https://creativecommons.org/licenses/by/4.0/deed.zh) 本文作者: 苏洋 创建时间: 2012年09月27日 统计字数: 597字 阅读时间: 2分钟阅读 本文链接: https://soulteary.com/2012/09/27/count-overflow-view-port-elements.html ----- # 输出页面所有宽度大于等于视区的元素 其实是求解,这个到底是神马bug。 明天到公司再想一想,这个到底是那里的问题,顺便改一下这个脚本,我的机器IE7的控制台是各种跑,各种死。 bug复现地址:`http://sae.sina.com.cn/?m=front&a=rate` 多谢 chjund 提醒,将 `document.width` 替换为 `document.body.clientWidth` 了。 ```js var a = document.getElementsByTagName('body')[0]; var a = a.getElementsByTagName('*'); var w = q = r = 0, z = ''; w = document.body.clientWidth; for (i = 0; i < a.length; i++) { if (a[i].nodeName != 'SCRIPT') { r = a[i].offsetLeft + a[i].offsetWidth; if (w <= r) { q++ z += 'Overflow Element:@TYPE[' + a[i].nodeName + '], @INDEX[' + i + '], @VALUE[' + r + ']' + '\n' } } } console.log('Total Elements:', a.length, '(', (q / a.length * 100).toFixed(2) + '%', ')') console.log(z) ```