簡體   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