【问题标题】:How to change jQuery code to hide .jpg at the end of the url?如何更改 jQuery 代码以在 url 末尾隐藏 .jpg?
【发布时间】:2013-01-27 13:39:38
【问题描述】:

有人可以帮我编写这段简短的代码吗? 现在,当我在我的网站上单击缩略图时,它会在地址栏中创建 url。但 url 最后有 .jpg 。 我应该如何修改代码,使其不会在 url 末尾显示 .jpg?

如果用户使用 www.domain.com/#image.jpg 之类的 url 进入站点,这段代码也会自动打开 Colorbox 图像,因此自然地对代码的更改也会影响到这一点。

谢谢!

jQuery(function() {
    var id, group;

    group = jQuery("a[rel='lightbox[63]']").colorbox({ 
        onComplete: function() {
            window.location.hash = (this.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1];
        }, 
        onClosed: function() {
            location.hash = '';
        }
    });

    id = location.hash.replace(/^\#/, '');
    group.filter('[href$="'+id+'"]').eq(0).click();
});

【问题讨论】:

    标签: jquery colorbox


    【解决方案1】:

    替换这个:

    window.location.hash = (this.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1];
    

    有了这个:

    // Get the image URL
    with_ext = (this.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1];
    // Get the image url without the extension
    without_ext =   with_ext.substring(0, with_ext.lastIndexOf(".")));
    // Redirect
    window.location.hash = without_ext;
    

    说明

    示例

    http://jsbin.com/uqufev/1/edit

    【讨论】:

    • 感谢您的回答。如果我单击图像,它现在可以工作,但是当手动输入 url(最后没有 .jpg)时,它不会自动打开图像。知道如何实现吗?
    • 请链接到您的页面。 (PS:SA 不是论坛,它是 QA 网站,所以如果您的答案得到解决,并且您还有其他问题,请单独提出一个新问题,这样您可能会得到更好的答案。
    猜你喜欢
    • 2013-04-09
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 2020-10-31
    • 2013-01-10
    • 2016-07-19
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多