html中获取图片的真实尺寸

使用js得到图片的真实尺寸,下面就是js代码:

复制代码
<html>
    <head>
        <script type="text/javascript" src="jquery.js"></script>
    </head>
    <body>
            <img style="width:100px;height:100px;" id="img" src="4.jpg"/>
            <button onclick="ge();">点我送屠龙宝刀</button>
    </body>

    <script type="text/javascript">
        function ge(){
            var img = document.getElementById("img");
            //把这个对象进行克隆
            var ig = $(img).clone();
            //把css属性中的width与height去掉,然后就是它的真实尺寸了
            $(ig).css("width","");
            $(ig).css("height","")

            //这里把jquery转成了原生的js对象,来取它的宽度与长度
            alert("宽度:"+$(ig)[0].width+"    高度:"+$(ig)[0].height);
        }
    </script>
</html>
复制代码

把这个html复制回去,就可以直接使用了!



感谢您的阅读,如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮。本文欢迎各位转载,但是转载文章之后必须在文章页面中给出作者和原文连接
posted @   朱小杰  阅读(2992)  评论(1编辑  收藏  举报
点击右上角即可分享
微信分享提示