jquery設置div的最小寬度方法 jquery獲取元素的寬度

jquery獲取元素的寬度 , jquery設置div的最小寬度方法,金華號帶你了解相關信息 。
前端web頁面中 , js應該很方便的獲取div元素的高度和寬度 , 那么這篇文章就說一說原生JS與JQ怎么樣急速的獲取DIV元素的高度和寬度的方法 。

jquery設置div的最小寬度方法 jquery獲取元素的寬度

文章插圖
js獲取div元素高度與寬度的方法js獲取div元素的高度與寬度要用的 clientHeight 與 clientWidth方法
clientHeight:返回元素的可視高度
clientWidth:返回元素的可視寬度
示例代碼:<div id="mochu" style="height: 200px;width:150px;color:#fff;background-color: blueviolet;">
趣玩號
</div>
<script>
//獲取高度
var h = document.getElementById('mochu').clientHeight;
//獲取寬度
var w = document.getElementById('mochu').clientWidth;
console.log(h);
console.log(w);
</script>
打印結果:
200
150
jQuery獲取div元素高度與寬度的方法
相對于原生的 javascript 來說 , 使用 jquery 來獲取 div 元素的高度與寬度要無腦的多 。
jq獲取div元素寬度的方法
$(selector).width()
jq獲取div元素高度的方法$(selector).height()
示例代碼:
<div id="mochu" style="height: 100px;width:150px;">
http://www.feiniaomy.com
</div>
<script>
//獲取高度
var h = $('#mochu').height();
//獲取寬度
var w = $('#mochu').width();
console.log(h);
console.log(w);
</script>
打印結果:
100
【jquery設置div的最小寬度方法 jquery獲取元素的寬度】150

    推薦閱讀