gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:36:29 26 4
gpt4 key购买 nike

我的函数检查 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com