简体   繁体   English

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

[英]Hide all elements with a specific class and show by ID

I have all elements of a specific class being hidden upon page load using jquery's hide() function. 我使用jQuery的hide()函数在页面加载时隐藏了特定类的所有元素。 I am trying to display the element again based upon it's ID when a link is clicked. 我试图在单击链接时根据其ID再次显示该元素。

There are 7 elements with the class being hidden, each with a different ID. 该类有7个元素被隐藏,每个元素都有不同的ID。 When the link containing href="#element-id" is clicked it should show only that element and keep all other elements hidden. 单击包含href="#element-id"的链接时,该链接应仅显示该元素,而将所有其他元素隐藏。

Here is my current code hiding the elements: 这是我当前隐藏元素的代码:

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

And here is what I have that is supposed to display the correct element on click: 这就是我应该在单击时显示正确元素的内容:

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

Right now nothing happens when clicking on the element. 现在,单击该元素时没有任何反应。 How can I keep all elements with the class menu-wrapper hidden while only the element with the class menu-wrapper and the correct ID is visible? 如何仅隐藏具有menu-wrapper类和正确ID的元素,而隐藏所有具有menu-wrapper类的元素?

Try 尝试

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM