作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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));
});
}
}
最佳答案
代码行:
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
关于javascript - 哈希和数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12158579/
我是一名优秀的程序员,十分优秀!