1 | var widthValue = $(selector).width(); |
1 | var widthValue = $( '#myDiv' ).width(); |
1 | var heightValue = $(selector).height(); |
1 | var heightValue = $( '.myClass' ).height(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <title>使用.width()和.height()获取元素的宽度和高度</title> <script src= "https://zz.bdstatic.com/linksubmit/push.js" ></script><script src= "https://hm.baidu.com/hm.js?09557d432c13273dfc40996016de61c4" ></script><script src= "https://code.jquery.com/jquery-3.6.0.min.js" ></script> <style> #myDiv { width: 300px; height: 200px; background-color: #f1f1f1; } </style> <div id= "myDiv" > <h1>Hello, World!</h1> </div> <script> var widthValue = $( '#myDiv' ).width(); var heightValue = $( '#myDiv' ).height(); console.log( '宽度:' + widthValue + 'px' ); console.log( '高度:' + heightValue + 'px' ); </script> |
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com