1

我正在尝试根据一天中的时间更改消息。

我已将我在 ST2 中查看的内容粘贴到JSFiddle

这里是:

<div class="container">
    <div id="time">
        <p id="time">Good morning.</p>
    </div>

<div>
    <p>I used to work in the marketing industry.<br>Now I'm a Web Designer and Developer.<br> I like to keep things simple.</p>
</div>​

查询:

<script type="text/javascript">
 $(document).ready(function()
    {
      var now = new Date();
      var hours = now.getHours();
      var msg;
      if(hours<12)
           msg = "Good Morning";
      else if(hours<18)
           msg = "Good Afternoon";
      else
          msg = "Good Evening";
      return(msg);
    });
});
</script>​

当谈到 jquery 时,我是一个完全的菜鸟,所以要温柔。

HTML:

<!DOCTYPE html>
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

  <script type="text/javascript">
      $(document).ready(function() {
      var now = new Date();
      var hours = now.getHours();
      var msg;
      if (hours < 12) msg = "Good Morning";
      else if (hours < 18) msg = "Good Afternoon";
      else msg = "Good Evening";
      $('#time p').text(msg);
    });
});
</script>

</head>

<body>

<div class="container">
    <div id="time">
        <p>Good morning.</p>
    </div>

<div>
    <p>I used to work in the marketing industry.<br>Now I'm a Web Designer and Developer.<br> I like to keep things simple.</p>
</div>​
</div>

</body>
4

2 回答 2

2

只需更改return msg$('#time p').text(msg);

演示:http: //jsfiddle.net/EVCMm/10/

这使用jQuerytext()方法来替换文本中寻找p标签ID=time

API 参考: http ://api.jquery.com/text/

于 2012-11-10T19:25:15.797 回答
-4
$('#time').html('<p>'+msg+'</p>');
于 2012-11-10T19:21:34.510 回答