2010-01-14 20 views
0

我在下面的陳述中關注的陳述是fancybox = 1;如果我創建的任何臨時圖像的最大寬度超過480,則需要執行此操作。只適用於及時操作,爲什麼?

有關與此相互作用的html的一些背景信息: 我有一個包含圖像的鏈接。
圖像是一個調整大小的版本,鏈接的href是原始的,未處理的圖像。
有一系列這些鏈接包裝的圖像
div,addon-large-image,包裝整個事情。

由於某些原因,如果我有'alert(m);'這個代碼,包括在內。我正確地結束了我最後的if語句(在這種情況下,我確實有圖像寬於480),並且我得到的最後一個警告是「已觸發」。但是,如果我註釋掉'alert(m);',並且沒有其他改變,'alert(「Triggered」);'未能開火,顯示我事實上並未進入我最後的條件。

我在做什麼錯在這裏有什麼想法嗎?我在Java中有一個編程背景,但是對於Jquery而言,我從任何重要的角度來說都是新手,所以我猜測我有一種類似'alert(m);'的語法問題。有點意外的修復。

'最高'在我的問題範圍內是無關緊要的,它做到了正確的應用,在其他地方使用,並且在我實現maxWidth之前就已經存在。

var tallest = 0; 
    var tempImg = new Image(); 
    var tempSrc = ""; 
    var maxWidth = 0; 

    // Finds the tallest image in the set. 
    $("#addon-large-image img").each(function() { 
     var n = $(this).attr("height"); 
     if (tallest < n) { 
      tallest = n; 
     } 

     tempSrc = $(this).parent().attr("href"); 
     $(tempImg).attr("src", tempSrc); 

     var m = $(tempImg).attr("width"); 

     alert(m); 

     if (maxWidth < m) { 
      maxWidth = m; 
     } 
    }); 

    if (maxWidth > 480) { 
     fancybox = 1; 
     alert("Triggered"); 
    } 
+0

使用火狐? – 2010-01-14 19:37:13

+0

在Firefox和Safari中測試 – Jacob 2010-01-14 19:48:32

回答

1

看起來好像還沒有完全載入你的腳本。嘗試在jQuery document.ready中運行此操作,看看它是否有效。

+0

它適用於警報,因爲在關閉警報時,您的對象已完全加載。 – nickytonline 2010-01-14 19:32:48

+0

同意,首先想到的是競賽條件 – pstanton 2010-01-14 19:33:40

+0

nickyt, 這位於document.ready中。 – Jacob 2010-01-14 19:33:40

0

你使用Ajax Request嗎?

如果是,那麼把你的代碼,在

AjaxObject.onreadystatechange = function() { 
     if (AjaxObject.readyState == 4) { 
      . 
      . 
      . 
      . 
      . 

     // PUT HERE 
     } 
    } 

末沒有工作,那麼如果沒有

使用本:

function testName() 
{ 
$(document).ready(function(){ 
    var tallest = 0; 
    var tempImg = new Image(); 
    var tempSrc = ""; 
    var maxWidth = 0; 

    // Finds the tallest image in the set. 
    $("#addon-large-image img").each(function() { 
     var n = $(this).attr("height"); 
     if (tallest < n) { 
      tallest = n; 
     } 

     tempSrc = $(this).parent().attr("href"); 
     $(tempImg).attr("src", tempSrc); 

     var m = $(tempImg).attr("width"); 

     alert(m); 

     if (maxWidth < m) { 
      maxWidth = m; 
     } 
    }); 
}); 
return true; 
} 

希望可以幫助您哥們

相關問題