哈希和数字

时间:2012-08-28 11:44:39

标签: javascript jquery hash numbers responsive-design

我在jQuery Elastislide中有一个画廊。

画廊的每张照片都有相应的哈希。

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

因此,当我重新加载第四张图片时,页面会加载第四张图片。

但是当我在Hash中没有标题之前重新加载时,图片无法加载。

* 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));
        });
    }
}​

1 个答案:

答案 0 :(得分:2)

代码行:

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

将尝试将哈希的第一个char转换为int,但如果第一个char不是有效的int(如果你删除了4就像你说的那样),那么JavaScript会在那时出错,不再进一步。

同样 - 根据文档,0似乎不是parseInt()的有效选项。

编辑:替换为W3Schools的链接

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