How to get the width and height of an img element where width and height ha开发者_如何转开发ve not been set?
They don't have to be explicitly set by CSS:
var img = document.getElementById("my_img");
var height = img.height;
var width = img.width;
Just like that, you have the height and width of your image element.
Give an Id to the image(myImage
) in the HTML and then ,
<img src="myimagejpg" id="myImage"/>
the following script will give the actuall height and width of the image...
var myImage = document.getElementById("myImage ");
var myheight= myImage .height;
var mywidth = myImage .width;
精彩评论