2011-06-01 76 views
1

我试图显示两个标题之一,具体取决于某些DOM元素在页面上是否可用。出于某种原因,它不能正常工作......到目前为止,这里是live demo当Div可见时显示标题

我有下面的代码:

$('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存储中的任何好果子本周

EDIT 2 使用下面的代码试过,但nomatter其存储我选择排序,我只是得到错误信息,即使div的我正在寻找存在...

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

我看到了页面,你有什么要说的吗? – 2011-06-01 15:41:46

+0

刚刚添加了一些说明:) – 2011-06-01 15:47:56

+0

哦,你的意思是说,如果用户将组合框的选择更改为一个值,并且如果选定的下面没有子值,那么封装组合框的标题应该改变? – 2011-06-01 15:51:37

回答

1

尝试在你的改变这些线

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

此相反,移函数调用作为​​回调。

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

这看起来像一个很好的建议 - 运行时':visible'选择器选择的元素过多。 – Town 2011-06-01 16:23:02

+0

男人,我们在哪里6个小时前? :) - 谢谢,它似乎完美的工作! – 2011-06-01 16:29:23

+1

@TinyGiantStudios:如果一个方法有回调,这是有道理的。 – Marcel 2011-06-01 16:35:48

0

不知道这会帮助,但尝试的代码更改为:

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

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

坚果,只是试过 - 但现在我只收到错误消息,无论我选择哪个商店。任何其他想法? – 2011-06-01 15:49:44

+0

为什么它好?它不会隐藏不必要的块。 – 2011-06-01 15:50:52

+0

它与下拉菜单绑定。如果我选择一家商店,而且没有成果,那么它应该显示错误消息。现在确实发生了,但只有一次。如果我从下拉菜单中选择另一个选项,原始的错误将不会被h3.list_heading取代...我是否有意义? – 2011-06-01 15:57:42

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" : 
    ... 
    ... 
    ... 

我希望这个作品。上帝保佑!

+0

Jeepers,我永远不会想到这一点!我会用上面的解决方案做一些错误检查(目前为止这么好) - 但是如果我拿起任何东西,我肯定会尝试解决您的解决方案。非常感谢你对这个问题的看法... – 2011-06-01 16:40:43

相关问题