为什么这段代码运行结果不正确?

写了一段根据时间显示不同文字的代码,没有报错,但是运行结果不正确,明明本地时间是13点了,运行结果依旧显示上午好,到底是什么地方出现错误了呢

img

你这个获取的 div 是 个数组,要取第一个元素,

div[0].innerHTML ='早上好';

img

建议添加debugger进行调试,重点调试下分支条件判断代码

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7530863
  • 这篇博客你也可以参考下:富文本点击查看更多,如果内容超多出3行显示,未超过隐藏,显示所有富文本内容
  • 除此之外, 这篇博客: 实现搜索框点击时提示文字消失 离开时提示文字出现中的 该例为一个搜索框,点击搜索框后默认提示文字自动清除,离开再次显示默认提示文字 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • <html>
    <body>
    <meta charset="utf-8" />
    <title>CSS搜索框</title>
    <style type="text/css">
        .searchinput{
        border-right-width: 0px;
        padding-left: 3px;
        width: 168px;
        font-family: arial;
        float: left;
        border-top-width: 0px;
        border-bottom-width: 0px;
        color: #636365;
        margin-left: 4px;
        font-size: 8pt;
        vertical-align: middle;
        border-left-width: 0px;
        margin-right: 3px;
        }
        .tab_search{
        border-bottom: #cccccc 1px solid;
        border-left: #cccccc 1px solid;
        height: 25px;
        border-top: #cccccc 1px solid;
        border-right: #cccccc 1px solid;
        }
        .searchaction{
        width: 28px;
        float: left;
        height: 21px;
        font-size: 14px;
        padding: 1px 1px 1px 1px ;
        }
    </style>
    <form action="#" name="search">
        <table border="0" cellpadding="0" cellspacing="0" class="tab_search">
            <tr>
            <td>
                <input type="text" name="q" title="Search" class="searchinput" id="searchinput" value="请输入商家名称地址" onkeydown="if (event.keyCode==13) {}" onblur="if(this.value=='')value='请输入商家名称地址';" onfocus="if(this.value=='请输入商家名称地址')value='';" value="请输入商家名称地址" />
            </td>
            <td>
            <input type="submit" value="ok" width="21" height="17" class="searchaction" />
            </td>
            </tr>
        </table>
    </form> 
    
    </body>
    </html>
    

  • 您还可以看一下 孙玖祥老师的图解数据结构与算法课程中的 背包问题代码实现小节, 巩固相关知识点