Selecting elements in jQuery

眉间皱痕 提交于 2020-01-24 01:46:06

问题


In continuation of the question at this link I wanted to add some functionality in test.html
The event trigger is 'clicking' on any <p class=""> tag. After clicking I wanted that particular <p> to disappear (which was fairly straightforward) but I also want that

  1. If out of the three either one or two <p> are hidden, then "Hide 'em" button should 'show' along with "Show 'em" But
  2. If all <p> are hidden then "Hide 'em" should also get Hidden And
  3. When all <p> are visible then "Show 'em" should get Hidden.

I tried to go about the thing with my knowledge of jQuery selectors and the selector documentation avaliable, but didn't reach anywhere near what I want. This is what I have done till now.

$('p.*').live('click', function() {
        $(this).hide('slow');
        if( $('p').is(':hidden') ) {
            $('.shower').show();
        }
        if( $('p.*').is(':hidden') ) {
            $('.hider').show();
        }

    });

However This code does not selectively toggle at the extreme conditions of all <p> hidden


回答1:


Here's a way to do it

var all_ps = $('p.*').length;
var hidden_ps = 0;

$('p.*').live('click', function() {
    $(this).hide('slow');
    hidden_ps++;
    updateButtons();
});
function updateButtons()
{
    $('.hider').show();
    $('.shower').show();
    if(hidden_ps == all_ps-1) {
        $('.hider').hide();
    } else if(hidden_ps == 0) {
        $('.shower').hide();
    }
}
function resetButtons()
{
    $('p.*').show();
    hidden_ps = 0;
    updateButtons()
}


来源:https://stackoverflow.com/questions/1420774/selecting-elements-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!