[英]How to get exact width and height of the div
here i am going to ask two questions... 在这里我要问两个问题...
how to get the exact width of the div 如何获得div的确切宽度
Code: 码:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("Width of div: " + $("#a").width());
});
});
</script>
</head>
<body>
<div id="a" style="width:1000px;height:500px;">
<div style="height:100px;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;">
</div>
</div>
<br>
<button>Display the width of div</button>
</body>
</html>
here i am declared 2 .. parent div width is 1000 child div width is 300 if i use $("#a").width() function i will get 1000 which i mentioned on that div. 在这里我被声明为2 ..如果我使用$(“#a”)。width()函数,则父div的宽度为1000子div的宽度为300。我将在那个div上得到1000。 but how can i get the exact used width(300) using $("#a") as well as same in height?
但是如何使用$(“#a”)以及高度相同来获得确切使用的width(300)?
your "a" is 1000px - since you want the width of the div within the "a" you need to use: 您的“ a”为1000像素-由于您希望div的宽度在“ a”内,因此您需要使用:
$("#a div").width()
or if you can have multiple divs - then use :first (or some other selector) 或者如果您可以有多个div,则使用:first(或其他选择器)
You can use attr
methods like 您可以使用
attr
方法,例如
$("button").click(function(){
alert("Width of div: " + $("#a div").attr("width"));
});
尝试在jQuery中链接
$("#a div").width()
Use can use this code to get the height and width div inside #a div 可以使用此代码获取#a div中的高度和宽度div
$(document).ready(function(){ $("button").click(function(){ alert("Width of div: " + $("#a div").width()); alert("Width of div: " + $("#a div").height()); }); });
If you would have only two divs, then why dont you just assign id to your second div also. 如果您只有两个div,那么为什么不将ID也分配给第二个div。
<div id="a" style="width:1000px;height:500px;">
<div id="b" style="height:100px;width:300px;padding:10px;margin:3px;border:1px solid blue;background- color:lightblue;">
</div>
</div>
Then you can just get the width and height as follows: 然后,您可以按以下方式获取宽度和高度:
var width = $('#b').attr("width");
var height = $('#b').attr("height");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.