I have a HTML element but I have not defined the width or height (or sometimes I may not have defined the right or bottom, but I did define the width & height) in CSS.
Do you know how I can use JavaScript to obtain either an elements width or height or right or bottom if I haven't set it?
#blockMenu { z-index: 0; padding: 10%; }
If I do the following, I know it wont work because I didn't set the width or right:
var width = document.getElementById("blockMenu").offsetRight - document.getElementById("blockMenu").offsetLeft;
Is there an开发者_如何转开发yway to determine the dimensions & position of a element when I haven't set it?
If not what if I define the width; how do I obtain the elements width through JavaScript?
With JavaScript you can do
document.getElementById("blockMenu").clientWidth;
document.getElementById("blockMenu").clientHeight;
With jQuery you can do
$('#blockMenu').width();
$('#blockMenu').height();
Check working example at http://jsfiddle.net/7jteV/2/
if you have the option to use jquery, you can use the height() and width() methods.
You can use jQuery as well
$("blockMenu").width()
$("blockMenu").height()
http://api.jquery.com/width/
http://api.jquery.com/height/
document.getElementById("blockMenu").style.width
document.getElementById("blockMenu").style.height
Should do the trick
精彩评论