46

我有 JavaScript 代码,下面一行有问题。

if ((hr==20)) document.write("Good Night"); document.getElementById('Night).style.display=''

错误

Uncaught TypeError: Cannot read property 'style' of null at Column 69

我的 div 标签详细信息是:

    <div id="Night" style="display: none;">
    <img src="Img/night.png" style="position: fixed; top: 0px; left: 5%; height: auto; width: 100%; z-index: -2147483640;">
    <img src="Img/moon.gif" style="position: fixed; top: 0px; left: 5%; height: 100%; width: auto; z-index: -2147483639;"></div>

完整的 JavaScript:

    <script language="JavaScript">
    <!--
    document.write("<dl><dd>")
    day = new Date()
    hr = day.getHours()
    if ((hr==1)||(hr==2)||(hr==3)||(hr==4) || (hr==5)) document.write("Should not you be sleeping?")
    if ((hr==6) || (hr==7) || (hr==8) || (hr==9) || (hr==10) || (hr==11)) document.write("Good Morning!")
    if ((hr==12)) document.write("Let's have lunch?")
    if ((hr==13) || (hr==14) || (hr==15) || (hr==16) || (hr==17)) document.write("Good afternoon!")
    if ((hr==18) || (hr==19)) document.write("Good late afternoon!")
    if ((hr==20)) document.write("Good Night"); document.getElementById('Night').style.display=''
    if ((hr==21)) document.write("Good Night"); document.getElementById('Night').style.display='none'
    if ((hr==22)) document.write("Good Night")
    if (hr==23) document.write("Oh My! It's almost midnight!")
    if (hr==0) document.write("Midnight!<br>It is already tomorrow!") document.write("</dl>")
    //--->
    </script>

有人能帮我吗?

4

8 回答 8

92

在您的脚本中,这部分:

document.getElementById('Noite')

必须返回null,并且您还试图将display属性设置为无效值。第一部分有几个可能的原因null

  1. 您在加载文档之前过早地运行脚本,因此Noite无法找到该项目。

  2. 您的 HTML中没有Noite项目。

我应该指出,您document.write()在这种情况下使用代码可能表示存在问题。如果文档已经加载,那么 newdocument.write()将清除旧内容并开始一个新的新文档,因此不会Noite找到任何项目。

如果您的文档尚未加载,因此您正在进行document.write()内联以将 HTML 内联添加到当前文档,那么您的文档尚未完全加载,因此这可能是它找不到该Noite项目的原因。

解决方案可能是将脚本的这一部分放在文档的最后,以便在它之前的所有内容都已加载。所以把它移到你身体的末端:

document.getElementById('Noite').style.display='block';

并且,请确保document.write()加载文档后javascript中没有语句(因为它们会清除以前的文档并开始新的文档)。


此外,将display属性设置为"display"对我来说没有意义。有效的选项是"block", "inline", "none","table"等...我不知道有任何以"display"该样式属性命名的选项。请参阅此处display了解该属性的有效选项。

您可以在此演示中看到固定代码在这里工作:http: //jsfiddle.net/jfriend00/yVJY4/。该 jsFiddle 配置为将 javascript 放置在文档正文的末尾,以便在加载文档后运行。


PS我应该指出,您的if语句缺少大括号以及在同一行中包含多个语句会使您的代码非常具有误导性和不清楚。


我很难弄清楚你在问什么,但这是你的代码的清理版本,你也可以在这里看到它:http: //jsfiddle.net/jfriend00/QCxwr/。这是我所做的更改的列表:

  1. 该脚本位于正文中,但位于它所引用的内容之后。
  2. 我已经var为您的变量添加了声明(始终使用的好习惯)。
  3. if语句已更改为 if/else,这对于您正在做的事情来说效率更高,并且更能自我记录。
  4. 我为每个if语句添加了大括号,因此绝对清楚哪些语句是 the 的一部分,if/else哪些不是。
  5. 我已正确关闭</dd>您插入的标签。
  6. 我已更改style.display = '';style.display = 'block';.
  7. 我在每个语句的末尾添加了分号(另一个要遵循的好习惯)。

编码:

<div id="Night" style="display: none;">
    <img src="Img/night.png" style="position: fixed; top: 0px; left: 5%; height: auto; width: 100%; z-index: -2147483640;">
    <img src="Img/moon.gif" style="position: fixed; top: 0px; left: 5%; height: 100%; width: auto; z-index: -2147483639;">
</div>    
<script>
document.write("<dl><dd>");
var day = new Date();
var hr = day.getHours();
if (hr == 0) {
    document.write("Meia-noite!<br>Já é amanhã!");
} else if (hr <=5 ) {
    document.write("&nbsp;&nbsp;Você não<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;devia<br>&nbsp;&nbsp;&nbsp;&nbsp;estar<br>dormindo?");
} else if (hr <= 11) {         
    document.write("Bom dia!");
} else if (hr == 12) {
    document.write("&nbsp;&nbsp;&nbsp;&nbsp;Vamos<br>&nbsp;almoçar?");
} else if (hr <= 17) {
    document.write("Boa Tarde");
} else if (hr <= 19) {
    document.write("&nbsp;Bom final<br>&nbsp;de tarde!");
} else if (hr == 20) {
    document.write("&nbsp;Boa Noite"); 
    document.getElementById('Noite').style.display='block';
} else if (hr == 21) {
    document.write("&nbsp;Boa Noite"); 
    document.getElementById('Noite').style.display='none';
} else if (hr == 22) {
    document.write("&nbsp;Boa Noite");
} else if (hr == 23) {
    document.write("Ó Meu! Já é quase meia-noite!");
}
document.write("</dl></dd>");
</script>
于 2013-10-07T21:47:17.697 回答
4

发生这种情况是因为document.write会覆盖您现有的代码,因此将您的代码放在您div的 javascript 代码之前。例如:

CSS:

#mydiv { 
  visibility:hidden;
 }

在您的 html 文件中

<div id="mydiv">
   <p>Hello world</p>
</div>
<script type="text/javascript">
   document.getElementById('mydiv').style.visibility='visible';
</script>

希望这有帮助

于 2016-04-29T06:06:01.080 回答
2

只是我认为您的代码中缺少单引号

if ((hr==20)) document.write("Good Night"); document.getElementById('Night"here").style.display=''

应该是这样的

if ((hr==20)) document.write("Good Night"); document.getElementById('Night').style.display=''
于 2019-05-02T07:34:01.503 回答
2

晚上之后您缺少一个 ' (单引号)。就在这里getElementById('Night

于 2018-06-19T22:06:53.017 回答
1

对我来说,我的 Script 标签在 body 元素之外。在关闭body标签之前复制它。这有效

enter code here

<h1 id = 'title'>Black Jack</h1>
<h4> by Meg</h4>

<p id="text-area">Welcome to blackJack!</p>
<button id="new-button">New Game</button>
<button id="hitbtn">Hit!</button>
<button id="staybtn">Stay</button>

 <script src="script.js"></script>

于 2019-02-20T09:58:20.527 回答
0

错误背后的可能原因是 JavaScript 运行的时间远早于它需要从网页加载的对象。下面的代码肯定会解决问题。

if ( document.getElementById('Night) )
    document.getElementById('Night).style.display=''";

在对对象调用操作之前检查 DOM 对象是否存在始终是一个好习惯。

于 2021-10-17T04:21:41.843 回答
0

我遇到了同样的问题,情况是我需要通过embed标签下载flash游戏,通过iframe下载H5游戏,我需要一个加载框,当flash或H5下载完成后,让加载框显示无。好吧,闪光灯工作得很好,但是当事情进入 iframe 时,我找不到 null 的属性“样式”,所以我给它添加了一个时钟,它可以工作

let clock = setInterval(() => {
        clearInterval(clock)
        clock = null
        document.getElementById('loading-box').style.display = 'none'
    }, 200)
于 2018-03-16T08:33:22.173 回答
0

您的脚本来源可能位于不正确的位置。将指向您的 JS 页面的链接放在 /body 下。

    </body>
    <script src="Index.js"></script>
    </html>
于 2021-08-26T19:56:57.017 回答