Javascript 警报未显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12906155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:24:30  来源:igfitidea点击:

Javascript Alert not displaying

javascripthtml

提问by Jeremy B

I am working from the book "Simply JavaScript" and there is an example on pages 68-69 that I cannot get to run. I have copied it from the book and I am working with a fellow student. I think there must be a coding error in the example and was hoping someone could look it over real quick and give us some guidance.

我正在使用“Simply JavaScript”这本书,并且在第 68-69 页上有一个我无法运行的示例。我从书中复制了它,我正在和一个同学一起工作。我认为示例中肯定存在编码错误,希望有人能够快速查看并给我们一些指导。

From what I understand the script should cause an alert box to pop up and display the node name (which according to the book should just be a)

据我了解,脚本应该会弹出一个警告框并显示节点名称(根据本书应该是a

Here is the HTML

这是 HTML

<!doctype html>

<head>
<script type="text/javascript" src="script.js"></script>
<meta charset="utf-8">

<title>
Stupid Title
</title>
</head>

<body>

 <h1>
 Sniper (1998)
 </h1>

 <p> 
 In this cinema masterpiece
 <a id ="berenger" href="name/nm0000297/">tom Berenger</a> plays a us soldier working    in the Panamanian Jungle.
 </p>
</body>

And here is my JavaScript

这是我的 JavaScript

alert("AAAAAAAAAAAAAAHH");

var target = document.getElementById("berenger");
if (target != null)
    alert(target.nodeName);

This is my second week of javascript class so I'm pretty new with it.

这是我的 javascript 课程的第二周,所以我对它很陌生。

回答by gdoron is supporting Monica

The DOM isn't ready at this point:

此时 DOM 还没有准备好:

<script type="text/javascript" src="script.js"></script>

Move that line to the end of <body>

将该行移动到末尾 <body>

Read about those kind of issues in this docs

阅读本文档中的此类问题