【问题标题】:Hash and the Numbers哈希和数字
【发布时间】:2012-08-28 11:44:39
【问题描述】:

我在 jQuery Elastislide 中有一个画廊。

图库的每张图片都有一个对应的Hash。

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

【问题讨论】:

  • init = function() {Gallery = (function() { 函数没有右括号。

标签: javascript jquery hash numbers responsive-design


【解决方案1】:

代码行数:

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

【讨论】:

  • real documentation 供任何感兴趣的人使用。 w3schools is not documentation.
  • @jbabey 我喜欢 w3schools...尤其是测试... Javascript:只要您知道如何编写 cmets,就可以拧作用域、提升或关闭 :-D
  • 谢谢,我找到了解决方案,我只需要删除该行:imageIndex = parseInt(imageIndexStr, 0); // convert to int
  • 感谢文档指针...我知道 w3schools 不受欢迎
猜你喜欢
  • 2019-04-04
  • 1970-01-01
  • 2012-03-26
  • 2013-05-02
  • 2013-03-21
  • 2021-12-23
  • 2017-03-19
  • 2017-01-22
  • 2016-12-26
相关资源
最近更新 更多