2013-04-09 42 views
0

我有一個帶有日曆和一堆「Today in Weather History」信息的html文件。我想隱藏所有天氣歷史,直到有人點擊日曆上的特定日期,然後我只想揭示當天的歷史。如何使用jquery只顯示隱藏文本片段

每日天氣信息是隱藏的,但它不會顯示日曆日期被點擊的特定日期。我假設我正在談論這一切都是錯誤的。更好的方法來讚賞它將不勝感激。

日曆天鏈接:

<td align="right"><a id="c0408" href="#"> 8</a></td> 

天氣歷史:

<div id="allhistory"> 
    <div id="hc0408" style="display:none"> 
    <ul> 
     <li class="dateheader">April 8, 2007</li> 
     <li>The Savannah Airport drops to 28 degrees, its lowest April 
      temperature.</li> 
     <li class="dateheader">April 8, 2007</li> 
     <li>Summerville has their coldest April temperature, when the mercury 
      falls to 27 degrees.</li> 
    </ul> 
    </div> 

    <div id="hc0409" style="display:none"> 
    <ul> 
     <li class="dateheader">April 9, 2011</li> 
     <li>Severe thunderstorms impact Berkeley County in South Carolina, 
      with hail as large as tennis balls (2.5 inches) and winds as 
      high as 64 mph.</li> 
    </ul> <!-- 0409 --> 
    </div> 
</div> <!-- all history div --> 

jQuery代碼:

<script> 
    $(document).ready(function() { 
    $('#calendar a').click(function() { 
     var dateid = $(this).attr('id'); 
     var selection = "div#h"+dateid; 
     $("selection").show(); 
    }) 
    }); 
</script> 
+0

「P#H」< - 你的意思是DIV#H + – Bill 2013-04-09 17:08:54

+0

是的。我的意思是div#h – Jaystew 2013-04-09 17:50:33

回答

0

試試這個:

$(document).ready(function() { 
    $('#calendar a').click(function(e) { 
     e.preventDefault(); 
     $('#allhistory [id^="h"]').hide(); 
     $('#h'+$(this).attr('id')).css('display','inline-block'); 
    }) 
}); 
+0

太棒了!這工作。現在我只需要隱藏那裏有什麼,如果用戶點擊另一個日期。 – Jaystew 2013-04-09 17:44:19

+0

@ user2208943你想要,之後他再點擊一天你想隱藏舊的,並顯示剛剛按下的那一天? – 2013-04-09 17:55:41

+0

是的!就這樣。 – Jaystew 2013-04-09 17:56:57

0

串聯的ID,並顯示相應的jQuery對象

試這個

$('#calendar a').click(function(e) { 
    e.preventDefault(); //to prevent the default behaviour of a 
    var dateid = $(this).attr('id'); 
    var selection = $("#h"+dateid); //assign selection as jquery object 
    selection.show(); //show it 
    ..... 
0

$("selection").show();

這是你的問題。您正在提供字符串「選擇」作爲選擇器。你想提供這個變量;像這樣:

$(selection).show();