2014-10-29 60 views
0

我試圖讓我的頭腦爲什麼這不起作用,並想知道是否有人可以幫助我。基本上我需要不同圖像的這個對象的多個實例,我需要每個對象來存儲圖像的高度/寬度爲其相關的圖像進行進一步的操作,但onload事件永遠不會觸發?Javascript onload event not firing inside object

對不起,如果你看到TestTheVar函數imgW從來沒有設置任何東西,那麼你應該清楚繼承人的完整代碼。

<script type="text/javascript"> 

    (function() { 

    var myTest = new TestObj(); 
    mytest.TestTheVar(); 
    })(); 

function TestObj() { 
    this.img = new Image(); 

    this.img.onload = function(){ 
    this.imgW = this.img.width; 
    this.imgH = this.img.height; 
    }; 

    this.img.src = "reel_normal.PNG"; 
    this.TestTheVar = function() { 
    alert(this.imgW); 
    } 

} 
</script> 
+1

你是如何測試,如果onload事件觸發? – 2014-10-29 20:20:03

+0

進一步向下對象theres使用imgW它永遠不會被設置的函數 – 2014-10-29 20:22:43

+0

我有類似的代碼工作。我使用addEventListener,但我不認爲它有所作爲。請使用控制檯輸出進行仔細檢查。 – Sebas 2014-10-29 20:22:46

回答

1

您這裏有兩個問題

1)範圍

2)定時

範圍,在其他的答案中提到指的是事實,this內onload功能是Image對象,而不是您的TestObj,因此您需要執行以下操作:

<script type="text/javascript"> 

(function() { 

    var myTest = new TestObj(); 
    mytest.TestTheVar(); 
})(); 

function TestObj() { 
    var self = this; 
    this.img = new Image(); 

    this.img.onload = function(){ 
    self.imgW = this.width; 
    self.imgH = this.height; 
    }; 

    this.img.src = "reel_normal.PNG"; 

    this.TestTheVar = function() { 
    alert(this.imgW); 
    } 

} 
</script> 

時間是指當您嘗試訪問高度和寬度時,您無法假定圖像已完成加載。這是什麼回調是好的:

<script type="text/javascript"> 

    (function() { 

     var myTest = new TestObj(function() { 
      myTest.TestTheVar(); 
     }); 

    })(); 

function TestObj(cb) { 
    cb = cb || function() {}; 
    var self = this; 
    this.img = new Image(); 

    this.img.onload = function(){ 
    self.imgW = this.width; 
    self.imgH = this.height; 
    cb(); 
    }; 

    this.img.src = "reel_normal.PNG"; 

    this.TestTheVar = function() { 
    alert(this.imgW); 
    } 

} 
</script> 
+0

您可以在'onload'函數內實際執行'this.width'而不是'self.img.width' :-) – 2014-10-29 20:35:07

+0

我也修復了第二個示例,您將它留爲'this.img.width' ;-) – 2014-10-29 20:37:02

+0

非常感謝,這真的讓我從中清除了一些東西,這是我在javascript中的第一次投射,並且範圍正在給我帶來一點腦痛,完美的效果! – 2014-10-29 20:45:32

1

this是屬於每個功能的關鍵字。

load事件偵聽器中,它將成爲圖像,而不是您的TestObj實例。

因此,您可以

  • 使用this.img.imgW得到它:

    function TestObj() { 
        var that = this; 
        this.img = new Image(); 
        this.img.onload = function(){ 
        this.imgW = this.width; 
        this.imgH = this.height; 
        that.testTheVar(); 
        }; 
        this.img.src = "reel_normal.PNG"; 
        this.testTheVar = function() { 
        alert(this.img.imgW); 
        }; 
    } 
    
  • 存儲在您的TestObj例如:

    function TestObj() { 
        var that = this; 
        this.img = new Image(); 
        this.img.onload = function(){ 
        that.imgW = this.width; 
        that.imgH = this.height; 
        that.testTheVar(); 
        }; 
        this.img.src = "reel_normal.PNG"; 
        this.testTheVar = function() { 
        alert(this.imgW); 
        }; 
    } 
    
  • 自定義this的事件處理中成爲你的TestObj例如:

    function TestObj() { 
        this.img = new Image(); 
        this.img.onload = (function(){ 
        this.imgW = this.img.width; 
        this.imgH = this.img.height; 
        this.testTheVar(); 
        }).bind(this); 
        this.img.src = "reel_normal.PNG"; 
        this.testTheVar = function() { 
        alert(this.imgW); 
        }; 
    } 
    
+0

關閉,但'this.img'在'onload'函數內不起作用。 – 2014-10-29 20:34:19