Div可见时显示标题

时间:2011-06-01 15:39:24

标签: javascript jquery wordpress

我正在尝试显示两个标题中的一个,具体取决于页面上是否有某些DOM元素可用/可见。出于某种原因,它不起作用......到目前为止这是live demo

我有以下代码:

$('h3.list_heading').css('display', 'none');
$('h3#error').css('display', 'none');
    if ( $('div.availableNowListing').filter(':visible').length == 0) {
        $('h3#error').css('display', 'block');
    } else {
        $('h3.list_heading').css('display', 'block');
    }

目前,无论我选择什么,我只会显示一个标题...

修改 只是为了解释会发生什么: 单击要排序的商店时,它应仅显示与该商店关联的条目。如果没有与该商店相关的水果,标题为:

我们对本周xxxxx中最佳可用的建议

应该改为

运气不好!看来我们本周在xxxxx商店找不到任何好果子

编辑2 尝试使用下面的代码,但无论我选择哪个商店进行排序,我只是得到错误信息,即使我正在寻找的div存在...

$('h3.list_heading').hide();
$('h3#error').hide();
if ( $('div.availableNowListing:visible').length) {
    $('h3#error').show();
} else {
    $('h3.list_heading').show();
}

3 个答案:

答案 0 :(得分:1)

尝试更改switch

中的这些行
$('div.availableNowListing').not(':first').find('div.available_now_entry').fadeOut('fast');
check_heading();

相反,将函数调用转移为fadeOut()的回调。

$('div.availableNowListing').not(':first').find('div.available_now_entry').fadeOut('fast',  check_heading);

答案 1 :(得分:0)

不确定它是否有帮助,但请尝试将代码更改为:

$('h3.list_heading').hide();
$('h3#error').hide();

if ($('div.availableNowListing:visible').length) {
    $('h3#error').show();
} else {
    $('h3.list_heading').show();
}

答案 2 :(得分:0)

function onscreen_sort (get_store) {
  var check = false;
  $('div.availableNowListing').each( function() {
      // Reset Display
      var correct = $(this).children('.' + get_store).not(':first-child')
      var incorrect = $(this).children(':not(.' + get_store + ') ').not(':first-child')

      if(correct.length > 0) record = true;

      correct.find(":hidden").fadeIn('fast');  //only hide which is not correct ->less dom overlow
      incorrect.find(":visible").fadeOut('fast'); //any only show which is correct but hidden
  });
  check_heading(check)
}

function check_heading(boo) {
  $('h3#error').hide();
  $('h3.list_heading').hide();    

  if (boo) {
      $('h3.list_heading').show();
  } else {
      $('h3#error').show();
  }
}

switch ( store_selected ) {
  case "all" : 
      var class_match = "in_all"
      onscreen_sort(class_match);
      $('span.store_change').text( ' All Stores' );
      $('div.availableNowListing').not(':first').find('div.available_now_entry').fadeOut('fast');
      //check_heading();  //no more needed!
      break;

  case "asda" : 
     ...
     ...
     ...

我希望这有效。上帝好运!

相关问题