2010-09-03 59 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>