日期:2014-05-16  浏览次数:20405 次

请教一句代码
<img src="http://www.baidu.com/img/baidu_jgylogo3.gif" onload="alert(this.height)"> 

我想把后面的onload="alert 改成一个文本框,文本框里面是这个this.height的值。请问可以吗?

另外,如果有多个图片,能不能也能用?

------解决方案--------------------
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>    
        <style>
        </style>        
    </head>
    <body>
        <p>
            <img src="http://www.baidu.com/img/baidu_jgylogo3.gif" />
            <input />
        </p>
        <p>
            <img src="http://avatar.profile.csdn.net/2/3/6/2_missccc.jpg" />
            <input />
        </p>
        <script>
            function $(el){
                return typeof el == 'string' ? document.getElementById(el) : el;
            }
            function $t(name, cot){
                cot = cot || document;
                return cot.getElementsByTagName(name);
            }
            var img = $t('img');
            for(var i = 0, len = img.length; i < len; i++){
                img[i].onload = function(){
                    $t('input', this.parentNode)[0].value = '图片高度: '+this.offsetHeight;    
                }
            }
        </script>
    </body>
</html>