繁体   English   中英

使用javascript获取图像宽度有问题吗?

[英]Getting image width using javascript have problem?

这是我从网上得到的用于获取图像宽度的代码。 效果很好。

<script type="text/javascript">
function CreateDelegate(contextObject, delegateMethod)
{   
        return function()
            {
                return delegateMethod.apply(contextObject, arguments);
            }
}


function imgTesting_onload()
{
    image_width=this.width;
    alert(image_width);//First Alert
}
</script>
var imgTesting = new Image();
imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
imgTesting.src = "${str_applicationPath}/common/images/reports/"+report+".png"
//Second Alert//

但是我需要在上面的代码中// Second Alert //处访问image_width变量。我没有在那里获得width值,需要做些什么来获得那里的值吗?

请帮助我.........在此先感谢

那不可能 在那时,宽度是未知的。 这就是onload事件的重点。 您必须改为在imgTesting_onload的“第二个警报”中执行所需的代码。

您可以使用imgTesting.width获取图像宽度。

例如:

var imgTesting = new Image();

imgTesting.src =>“ $ {str_applicationPath} / common / images / reports /” + report +“。png”

//第二警报//

alert(imgTesting.width);

下面的代码不是必需的

function CreateDelegate(contextObject, delegateMethod)
{   
        return function()
            {
                return delegateMethod.apply(contextObject, arguments);
            }
}
function imgTesting_onload()
{
    image_width=this.width;
    alert(image_width);//First Alert
}
imgTesting.onload =CreateDelegate(imgTesting, imgTesting_onload);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM