【发布时间】:2024-01-02 14:30:01
【问题描述】:
当我在 html 文件内部编写 js 代码时,它运行良好。 这里是html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="./cs.css" rel="stylesheet">
<title>Document</title>
</head>
<body>
<div id="root" onload="App">
</div>
<script src="js/App.js" type="module" />
</body>
</html>
这里是 App.js 文件
import Insert from './Insert.js';
function App(){
document.getElementById("root").insertAdjacentHTML("afterbegin",'<h1>hello Javascript</h1>');
};
【问题讨论】:
-
调用函数需要括号:
onload="App()" -
另外,
onload不会在 DIV 上触发。将其移至<body> -
您好 Atiye Naderi,欢迎来到 Stack Overflow。您是否尝试过搜索问题?
标签: javascript module external-js