开发者

Get image width/height units in JavaScript

开发者 https://www.devze.com 2023-02-28 20:58 出处:网络
I am creati开发者_JS百科ng an image object in JavaScript as myImg = document.createElement(\'<img id=preview_image src=\"src/myImage.jpg\" align=\"baseline\" width=\"50%\" height=\"80%\"/>\');

I am creati开发者_JS百科ng an image object in JavaScript as

myImg = document.createElement('<img id=preview_image src="src/myImage.jpg" align="baseline" width="50%" height="80%"/>');

The image HTML will be obtained from database as in the given form. Now, how can I get the height and width with the units (i.e. 50% for width in this case)?


var width = myImg.width,   // width = '50%'
    height = myImg.height; // height = '80%'

// or
width = myImg.getAttribute('width'),
height = myImg.getAttribute('height');


As documented every DOMElement has a ClientHeight and ClientWidth property, those will have the values you need.

https://developer.mozilla.org/en/DOM/element

0

精彩评论

暂无评论...
验证码 换一张
取 消