“当前不会命中断点。没有为该文档加载任何符号。”
“The breakpoint will not currently be hit. No symbols have been loaded for this document.”
我用下面的代码完全修改了这个问题,以显示我收到的示例错误。
Javascript.js 文件
var yourName = prompt("What is your name?");
if (yourName != null){
document.getElementById("sayHello").innerHTML = "Hello" + yourName;}
else {
alert("Please enter a name next time!");}
index.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript" src="Javascript.js"></script>
</head>
<body>
<p id="sayHello">hello</p>
</body>
</html>
上面的代码会提示我询问它将收到的名称的消息,但是使用 'yourName' 变量,document.getElement 行不会 运行 因为html 代码
与 'hello' 相同。我试图调试它以查看为什么它不会 运行 通过文档行代码,但是当我单击文档行上的断点时我收到一条消息,'The breakpoint will not currently be hit. No symbols have been loaded for this document.' 它指的是什么符号?我正在使用 Microsoft Visual Studio.
回答你的问题,为什么放在 html 的后期(在评论中):
当 DOM 设置完成后,脚本需要 运行 才能找到元素。
当前脚本 运行 在 html 可访问之前正在运行,因此 getElementById
将无法找到任何内容。
最简单的更正是将脚本放在 <body>
标记的末尾(在它引用的 html 之后;正如您在屏幕截图中所做的那样)。
另一种选择是仅当 DOM 完成加载时才使用脚本 运行:pure JavaScript equivalent to jQuery's $.ready() how to call a function when the page/dom is ready for it
我用下面的代码完全修改了这个问题,以显示我收到的示例错误。
Javascript.js 文件
var yourName = prompt("What is your name?");
if (yourName != null){
document.getElementById("sayHello").innerHTML = "Hello" + yourName;}
else {
alert("Please enter a name next time!");}
index.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript" src="Javascript.js"></script>
</head>
<body>
<p id="sayHello">hello</p>
</body>
</html>
上面的代码会提示我询问它将收到的名称的消息,但是使用 'yourName' 变量,document.getElement 行不会 运行 因为html 代码
与 'hello' 相同。我试图调试它以查看为什么它不会 运行 通过文档行代码,但是当我单击文档行上的断点时我收到一条消息,'The breakpoint will not currently be hit. No symbols have been loaded for this document.' 它指的是什么符号?我正在使用 Microsoft Visual Studio.
回答你的问题,为什么放在 html 的后期(在评论中):
当 DOM 设置完成后,脚本需要 运行 才能找到元素。
当前脚本 运行 在 html 可访问之前正在运行,因此 getElementById
将无法找到任何内容。
最简单的更正是将脚本放在 <body>
标记的末尾(在它引用的 html 之后;正如您在屏幕截图中所做的那样)。
另一种选择是仅当 DOM 完成加载时才使用脚本 运行:pure JavaScript equivalent to jQuery's $.ready() how to call a function when the page/dom is ready for it