JavaScript - 用于获得真实图像宽度的功能高度(跨浏览器) [英] JavaScript - function to get real image width & height (cross browser)

查看:111
本文介绍了JavaScript - 用于获得真实图像宽度的功能高度(跨浏览器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得真实的图像宽度& JavaScript函数的高度(跨浏览器)?

How to get real image width & height (cross browser) by JavaScript function ?

推荐答案

首先,如果你有更大机会回答你的问题我只是以更礼貌的方式询问它,并提供尽可能多的相关信息。

First of all, you have a greater chance of getting your question answered if you'd just ask it in a more polite way, and supplying as much relevant information as possible.

无论如何......

Anyway...

据我所知,您可以在几乎所有浏览器中使用 .width 属性

For as far as I know, you can use the .width property across pretty much all browsers:

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

这篇关于JavaScript - 用于获得真实图像宽度的功能高度(跨浏览器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆