2012-08-28 68 views
1

我在jQuery Elastislide中有一個庫。哈希和數字

畫廊的每張圖片都有相應的哈希值。

例如:* www.example.com/gallery.html#4/title_of_the_picture *

所以,當我重新加載第四圖像,頁面加載第四圖像。

但是,當我重新加載沒有在哈希標題之前的數字,圖片不加載。

* www.example.com/gallery.html#title_of_the_picture *

我可以刪除這個號碼?如果可以,Jquery中的正確代碼是什麼?

jQuery代碼:

Gallery = (function() { 
    // index of the current item   
    var imageIndex = 0; 
    if (window.location.hash) { 
     var imageIndexStr = window.location.hash.replace('#', ''); // remove # 
     imageIndex = parseInt(imageIndexStr, 0); // convert to int 
    } 

    var current = imageIndex; 
    // mode : carousel || fullview 
    mode = 'carousel', 
    // control if one image is being loaded 
    anim = false, init = function() { 
     // (not necessary) preloading the images here... 
     $items.add('<img src="ajax-loader.gif"/><img src="black.png"/>').imagesLoaded(function() { 
      // add options 
      _addViewModes(); 
      // add large image wrapper 
      _addImageWrapper(); 
      // show first image 
      _showImage($items.eq(current)); 
     }); 
    } 
}​ 
+0

有你'的init =功能)不關閉括號({'和'圖庫=(函數(){'功能 –

回答

2

的代碼行:

var imageIndexStr = window.location.hash.replace('#', ''); 
imageIndex = parseInt(imageIndexStr, 0); // convert to int 

要儘量哈希的第一個字符轉換爲int,但如果第一個字符是不是有效的int(就像你說的那樣刪除了4),那麼JavaScript就會在這個時候出錯,不會再進一步​​。

另外 - 根據文檔,0似乎不是parseInt()的有效選項。

編輯:替換鏈接到W3Schools的

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/parseInt

+1

的[真正的文檔](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/parseInt)。[w3schools不是文檔](http://w3fools.com/)。 – jbabey

+0

@jbabey我喜歡w3schools ...特別是測試... Javascript:螺絲範圍,提升或關閉,只要你知道如何寫評論:-D – Christoph

+0

謝謝,我找到解決方案,我只需要刪除這一行:'imageIndex = parseInt(imageIndexStr,0); //轉換爲int' – Hedra