require.js 使用
1 <!DOCTYPE html>
2 <html lang="en">
3
4 <head>
5 <meta charset="UTF-8">
6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 <meta http-equiv="X-UA-Compatible" content="ie=edge">
8 <script src="https://cdn.bootcss.com/require.js/2.3.5/require.js"></script>
9 <title>Document</title>
10 </head>
11
12 <body>
13
14 <button id="require">点击require确定啊</button>
15
16 <script>
17 require.config({
18 // 当前目录
19 baseUrl: "./",
20
21 // 定义js,不用写后缀js
22 paths: {
23 "jquery": "http://code.jquery.com/jquery-2.1.1.min"
24 },
25
26 // 请求等待时间
27 waitSeconds: 15
28 });
29
30 // 使用jquery
31 require(["jquery"],
32 function (jquery) {
33 // 逻辑代码
34 $('#require').on('click',function(){
35 console.log('hehei');
36 });
37 }
38 );
39 </script>
40 </body>
41
42 </html>
至于,我们为什么用require.js(大概也就是js阻塞作用的原因吧,一旦某些JavaScript代码出现异常,页面肯定加载的很好看)
更多参数,参考官网API:http://www.requirejs.cn/
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步