在jQuery中选择元素 [英] Selecting elements in jQuery
问题描述
继续此链接上的问题我想要在test.html中添加一些功能
事件触发器是点击任何< p class =>标签。
点击后我想要那个特别的< p>消失(这是相当简单的)但是我也想要那个
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
- 如果三个中的任何一个或者两个< p>隐藏,然后隐藏按钮应显示以及显示'em但
- 如果全部< p>隐藏然后隐藏它们也应隐藏和
- 当所有< p>是可见的然后显示'em应该被隐藏。
我试着用我对jQuery选择器的知识和选择器文档可用,但没有达到我想要的任何地方。这就是我到目前为止所做的。
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();
}
});
但是,此代码无法在所有< p>的极端条件下有选择地切换。隐藏
However This code does not selectively toggle at the extreme conditions of all <p> hidden
推荐答案
这是一种方法
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()
}
这篇关于在jQuery中选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!