2010-09-03 57 views
1

感謝您對我以前的帖子@AndyE的幫助,您的解決方案效果很好。jQuery結合語句? (後續)

現在

我的跟進,同樣的想法,不同的功能(S)...我試圖執行以前的解決方案,但不可能得到它的工作吧...:

$(document).keypress(function(e) { 
    if (e.which == 27) { 
    $('#timeline-2010-1').hide(); 
    $('#timeline-2010-2').hide(); 
    $('#timeline-2010-3').hide(); 
    $('#timeline-2010-4').hide(); 
    $('#timeline-2010-5').hide(); 
    $('#timeline-2010-6').hide(); 
    $('#timeline-2010-7').hide(); 
    $('#timeline-2010-8').hide(); 
    $('#timeline-2010-9').hide(); 
    $('#timeline-2010-10').hide(); 
    $('#timeline-2010-11').hide(); 
    $('#timeline-2010-12').hide(); 
    $('#timeline-2010-13').hide(); 
    $('#timeline-2010-14').hide(); 
    $('#timeline-2010-15').hide(); 
    $('#timeline-2010-16').hide(); 
    $('#timeline-2010-17').hide(); 

    } 
}); 


$('a.close').click(function() { 
    $('#timeline-2010-1').hide(); 
    $('#timeline-2010-2').hide(); 
    $('#timeline-2010-3').hide(); 
    $('#timeline-2010-4').hide(); 
    $('#timeline-2010-5').hide(); 
    $('#timeline-2010-6').hide(); 
    $('#timeline-2010-7').hide(); 
    $('#timeline-2010-8').hide(); 
    $('#timeline-2010-9').hide(); 
    $('#timeline-2010-10').hide(); 
    $('#timeline-2010-11').hide(); 
    $('#timeline-2010-12').hide(); 
    $('#timeline-2010-13').hide(); 
    $('#timeline-2010-14').hide(); 
    $('#timeline-2010-15').hide(); 
    $('#timeline-2010-16').hide(); 
    $('#timeline-2010-17').hide(); 
    return false; 
    }); 


}); 

回答

3

我想給這些要素類,如:

<div id="#timeline-2010-1" class="timelineNode">Stuff</div> 

然後你就可以苗條下來到:

$(function() { 
    $(document).keypress(function(e) { 
    if (e.which == 27) { 
     $('.timelineNode').hide(); 
    } 
    }); 
    $('a.close').click(function() { 
    $('.timelineNode').hide(); 
    return false; 
    }); 
}); 
1

嘗試使用"[attr^='val']"選擇器模式(實際上是開始)。

$('a.close').click(function() { 
    $("[id^='timeline-2010-']").hide(); 
    return false; 
    }); 
0
$(function() { 
    $(document).keypress(function(e) { 
     $('[id^=timeline-]').hide(); 
    }); 

    $('a.close').click(function() { 
     $('[id^=timeline-]').hide(); 
     return false; 
    }); 
}); 

或者乾脆給這些元素共同類並使用類選擇器。

0

如果你想關閉所有的逃逸或緊密聯繫的開放元素的請嘗試以下操作:

<script> 
$(document).ready(function() 
{ 
    $("a.timeline-2010").click(function() 
    { 
     $(this).parent().children("div.timeline-2010").show(); 
     return false; 
    }); 
    $(document).keypress(function(e) 
    { 
     // firefox and IE for escape key 
     if (e.which == 27 || e.which == 0) 
     { 
      // hide all of the divs 
      $('div.timeline-2010').hide(); 
     } 
    }); 
    $('a.close').click(function() 
    { 
     $('div.timeline-2010').hide(); 
     return false; 
    }); 
}); 
</script> 

我的HTML低於:

<div> 
    <a class="timeline-2010" href="#">blah</a> 
    <div class="timeline-2010" style="display: none;"><a href="" class="close">Close</a> 
     Stuff that is hidden to be shown 
    </div> 
</div> 
<div> 
    <a class="timeline-2010" href="#">blah</a> 
    <div class="timeline-2010" style="display: none;"><a href="" class="close">Close</a> 
     Stuff that is hidden to be shown 
    </div> 
</div> 
<div> 
    <a class="timeline-2010" href="#">blah</a> 
    <div class="timeline-2010" style="display: none;"><a href="" class="close">Close</a> 
     Stuff that is hidden to be shown 
    </div> 
</div>