除了点击之外,如何切换每个jQuery元素?

时间:2010-07-17 09:44:22

标签: javascript jquery drupal

所以我试图在jQuery中实现看似非常基本的东西。我有一些“标题”,当我点击标题时,我希望该标题下方的信息向下滑动,并且所有其他标题信息向上滑动(这样一次只有一个标题显示信息)。

这是我想要发生的sudo代码:

$('.more-info-header:not(.jquery-processed)').addClass('jquery-processed').click(function(e) {
  $(this).children('.game-info-text').slideToggle("Slow");
  [all other ('.game-info-text')].slideUp(fast)
});

对于我的生活,我无法弄清楚如何编码第二行([all other('。game-info-text')])。非常感谢任何帮助!

(我将此实现为drupal行为,这就是为什么我要添加“jquery-processed”类的原因)

4 个答案:

答案 0 :(得分:3)

类似的东西:

$('.more-info-header:not(.jquery-processed)').addClass('jquery-processed').click(function(e) {
  $(this).children('.game-info-text').slideToggle("Slow");
  $('.more-info-header').not(this).children('.game-info-text').slideUp(fast);
  //                      ^-- removes the clicked element from all `.more-info-header`
});

参考:.not()

答案 1 :(得分:1)

听起来像是在jQuery UI Accordion插件之后。

答案 2 :(得分:1)

$('.more-info-header:not(.jquery-processed)')
.addClass('jquery-processed').click( function(e) {
  var these = $(this).children('.game-info-text').slideToggle("slow");
  $('.game-info-text').not(these).slideUp("fast");
});

使用not()功能,您可以排除已选择的元素。

答案 3 :(得分:1)

喜欢这个吗?

  $('.more-info-header').click(function(e){
    var target = e.target || e.srcElement;
   $(this).children().not(target).find('.game-info-text').slideUp('fast');
   $('.game-info-text', target).slideToggle("slow");
  });