代码描述:js获取宽度自适应元素值。js获取宽度自适应元素值源码下载
本章节介绍一下如何获取宽度自适应元素的宽度值,因为如果元素没有设置宽度的话,那么不论是getComputedStyle还是currentStyle的返回值都是auto,所以我们应该采取其他的手段迂回获取元素的宽度值。 代码实例如下:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="https://www.51qianduan.com/" /> <title>51前端</title> <style type="text/css"> body{ height:1000px; margin:20px; } #thediv{ width:100px; height:100px; background:red; position:fixed; top:100px; left:100px; border:50px solid blue; } input{margin-top:200px;} </style> <script type="text/javascript"> window.onload=function(){ var odiv=document.getElementById("thediv"); var obt=document.getElementById("bt"); obt.onclick=function(){ odiv.innerHTML=odiv.clientWidth; } } </script> </head> <body> <div id="thediv"></div> <input type="button" id="bt" value="查看效果"/> </body> </html>
请绑定手机号,在继续操作
注意:只有绑定手机以后,才可使用网站全部功能