HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blog</title>
<script type="text/javascript" src="scripts/abc.js"></script>
</head>
<body>
<p id="box">id</p>
</body>
</html>
JS code:
alert(document.getElementById("box").innerHTML);
Why can’t the above code be executed? There is no pop-up window when running it in Firefox browser. Any solution?
Dear, your js files should be placed below, the loading order is wrong
Because your code is not fully written, I guess you wrote the loading order in reverse
Execution timing
window.load look at this
Personally test that this code can pop up a window in Firefox. It's possible that your firefox prevents alert boxes from popping up.
Switch to
console.log(document.getElementById("box").innerHTML)
f12 to view the console output.Yes, as the previous brother said, your js comes in src before the body code. There was no dom at that time, so the node id=box cannot be obtained. This will report an error. One thing that those who learn js must know is to open the browser console to see if there is an error.
Did you write alert in abc.js? If this is the case, the element with id="box" cannot be obtained when alert is executed