JavaScript的 - 功能得到真正的图像的宽度和高度(跨浏览器)
-
22-07-2019 - |
题
如何由JavaScript功能得到真实图像的宽度和高度(跨浏览器)?
解决方案
首先,你必须让你的回答过的问题,如果你只是问它更礼貌的方式,并提供尽可能多的相关信息,尽可能的机会较大。
总之...
有关,据我所知,您可以在几乎所有的浏览器使用 .width
财产
function getDimensions(id) {
var element = document.getElementById(id);
if (element && element.tagName.toLowerCase() == 'img') {
return {
width: element.width,
height: element.height
};
}
}
<img id="myimage" src="foo.jpg" alt="" />
// using the function on the above image:
var dims = getDimensions('myimage');
alert(dims.width); --> shows width
alert(dims.height); --> shows height
其他提示
var realWidth = $("#image").attr("naturalWidth");
var realHeight = $("#image").attr("naturalHeight");
耶,谷歌的!
有几个方面,这取决于你所需要的东西(你已帮倒忙省略包括)做。可能是在一般意义上的最简单的是获得到图像对象的引用和检查width
和height
性质。
的jquery + <img src="" ... id="hello" />
+ $("#hello").width()
不隶属于 StackOverflow