javascript - jQuery 检索哈希并将其用作 ID

标签 javascript jquery hash filter if-statement

我的函数检查 URL 中的散列值,返回它的值,使用该值查找图像(按 ID)并为其应用类名。我的控制台没有显示任何错误,但我没有看到预期的结果。

它不是通过返回的 hash 值过滤 .z 类,而是将所有 .z 类写入 id =图片

是否有更好的方法来过滤该值并将其用作 ID?谢谢!

JavaScript:

(function($){
$.brantley = function(callback) {
    var $doc = $(document),
        $win = $(window),
        $z, $load, $footer, $header, $status, $container, $hash;

    $doc.ready(function() {
        $z          = $('.z');
        $load       = $('#load');
        $footer     = $('footer');
        $header     = $('header');
        $status     = $('#status');
        $container  = $('#container'),
        hash        = gethash();

        if(hash) {
            var image = hash;
            $('.z').attr('id', 'image').addClass('active');
        } else {
            $('.z:first').addClass('active');
        }

    });

    function gethash() {
        var hash=window.location.hash;
        hash=hash.replace(/#/,'');
        return hash;
    }

    function updateNumber(type) {
        window.location = window.location.pathname + "#" + type;
    }

};
})(jQuery);

编辑:

考虑了所有的评论和答案,这是我完成的:

(function($){
$.brantley = function(callback) {
    var $doc = $(document),
        $win = $(window),
        $z, $load, $footer, $header, $status, $container;

    $doc.ready(function() {
        $z          = $('.z');
        $load       = $('#load');
        $footer     = $('footer');
        $header     = $('header');
        $status     = $('#status');
        $container  = $('#container');

        var hash = gethash();

        if(hash) {
            $('#' + hash + '.z').addClass('active');
        } else {
            $('.z:first').addClass('active');
        }

        bromance();
    });

    $win.load(function() {
        bromance();
        $load.fadeOut('fast', function() {
            $('.active').fadeIn('slow');
            $status.fadeIn('slow');
            $footer.fadeIn('slow');
        });
    });

    $win.resize(function() {
        bromance();
    });

    function gethash() {
        var hash=window.location.hash;
        hash=hash.replace(/#/,'');
        return hash;
    }

    function updateNumber(type) {
        window.location = window.location.pathname + "#" + type;
    }
};
})(jQuery);

最佳答案

您不是按 ID 过滤,您是在更改 ID。要按 ID 过滤,请使用:

if(hash) {
    var image = hash;
    $('#' + image + '.z').addClass('active');
} else {
    $('.z:first').addClass('active');
}

或者干脆去掉那个多余的变量:

if(hash) {
    $('#' + hash + '.z').addClass('active');
} else {
    $('.z:first').addClass('active');
}

关于javascript - jQuery 检索哈希并将其用作 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7410144/

相关文章:

javascript - 我如何让 typescript 停止提示它不知道的功能?

javascript - 更改 ramda.js 中对象列表中的 prop

jQuery 创建具有属性差异的元素

c++ - 计算种子文件的信息哈希

javascript - Jest 测试经常失败,因为 Puppeteer 无法点击 DOM 中的元素

javascript - 用纯 Javascript 翻译基本的身份验证请求

javascript - 为 anchor 添加平滑滚动效果

javascript - highlight.js 内联模式可能吗?

c++ - 如何将 char 编码为 2 位?

hash - 使用 UUID 拆分在目录中均匀分布文件