如何使用jquery仅显示隐藏文本片段

如何使用jquery仅显示隐藏文本片段,jquery,show-hide,Jquery,Show Hide,我有一个带有日历的html文件和一堆“天气历史中的今天”信息。我想隐藏所有的天气历史,直到有人点击日历上的某一天,然后我只想显示那一天的历史 每日天气信息是隐藏的,但在单击日历日时,它不会显示特定的日期。我想这一切都错了。如果能以更好的方式解决这个问题,我们将不胜感激 日历日链接: <td align="right"><a id="c0408" href="#"> 8</a></td> 天气历史: <div id="allhistory

我有一个带有日历的html文件和一堆“天气历史中的今天”信息。我想隐藏所有的天气历史,直到有人点击日历上的某一天,然后我只想显示那一天的历史

每日天气信息是隐藏的,但在单击日历日时,它不会显示特定的日期。我想这一切都错了。如果能以更好的方式解决这个问题,我们将不胜感激

日历日链接:

<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 -->

    2007年4月8日
  • 萨凡纳机场降到28度,这是4月份的最低气温 温度
  • 2007年4月8日
  • 萨默维尔有着最冷的四月温度,这时水星 降到27度
    2011年4月9日
  • 严重雷暴影响南卡罗来纳州的伯克利县, 冰雹和网球一样大(2.5英寸),风和冰雹一样大 高达每小时64英里
jQuery代码:

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

$(文档).ready(函数(){
$(“#日历a”)。单击(函数(){
var dateid=$(this.attr('id');
var selection=“div#h”+dateid;
$(“选择”).show();
})
});

连接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
  .....
$(“选择”).show()

这是你的问题。您正在提供字符串“selection”作为选择器。您希望提供变量;像这样:

$(选择).show()

尝试以下操作:

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

“p#h”太棒了!成功了。现在我只需要隐藏如果用户点击另一个日期会出现什么。@user2208943你想要吗,在他点击另一天之后,你想要隐藏旧的日期并显示刚刚按下的日期吗?