gpt4 book ai didi

javascript - 隐藏具有特定类的所有元素并按 ID 显示

转载 作者:行者123 更新时间:2023-11-28 01:56:03 28 4
gpt4 key购买 nike

我使用 jquery 的 hide() 函数在页面加载时隐藏特定类的所有元素。我尝试在单击链接时根据元素的 ID 再次显示该元素。

有 7 个元素的类被隐藏,每个元素都有不同的 ID。单击包含 href="#element-id" 的链接时,它应该仅显示该元素并隐藏所有其他元素。

这是我当前隐藏元素的代码:

var menu = $('div.menu-wrapper');
menu.hide();

这是我应该在点击时显示正确元素的内容:

$('area').click(function() {
if($(this).attr('id') !== 'button') {
var target = $(this).attr('href');
target.toggle('slide', {
direction: 'right'
}, 900);
}
});

现在,单击该元素时没有任何反应。如何隐藏所有带有 menu-wrapper 类的元素,而只有带有 menu-wrapper 类且正确 ID 的元素可见?

最佳答案

尝试

var menu = $('div.menu-wrapper');
menu.hide();
//register the event handler to area elements other than #button
$('a:not(#button)').click(function () {
//hide all elements referred menu
menu.hide();
//get the target jQuery wrapper, the href need to start with #
var target = $($(this).attr('href'));
target.toggle('slide', {
direction: 'right'
}, 900);
});

关于javascript - 隐藏具有特定类的所有元素并按 ID 显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19097906/

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