简体   繁体   English

在jQuery中选择元素

[英]Selecting elements in jQuery

In continuation of the question at this link I wanted to add some functionality in test.html 此链接上继续提问时,我想在test.html中添加一些功能
The event trigger is 'clicking' on any <p class=""> tag. 事件触发器是在任何<p class =“”>标签上“单击”的。 After clicking I wanted that particular <p> to disappear (which was fairly straightforward) but I also want that 单击后,我希望特定的<p>消失(这很简单), 我也希望

  1. If out of the three either one or two <p> are hidden, then "Hide 'em" button should 'show' along with "Show 'em" But 如果三个或三个<p>中的一个被隐藏,则“隐藏'em”按钮应与“ Show'em”一起“显示”, 但是
  2. If all <p> are hidden then "Hide 'em" should also get Hidden And 如果所有<P>是隐藏的则“隐藏“EM”也应该得到隐藏的
  3. When all <p> are visible then "Show 'em" should get Hidden. 当所有<p>都可见时,则“ Show'em”应该被隐藏。

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. 我尝试着用我对jQuery选择器和选择器文档的了解来解决问题,但是没有达到我想要的目标。 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 但是,此代码不会在所有<p>隐藏的极端条件下有选择地切换

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

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

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