RequireJS 加载 jQuery 插件

jQuery 插件使用 RequireJS 加载,只需要在 shim 中配置插件依赖 jQuery 就可以了,下面以加载 SemanticUi 为例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>加载 jQuery 插件</title>
<link rel="stylesheet" href="//cdn.staticfile.org/semantic-ui/2.2.7/semantic.min.css">
</head>
<body>
<div class="ui selection dropdown">
<input type="hidden" name="gender">
<i class="dropdown icon"></i>
<div class="default text">Gender</div>
<div class="menu">
<div class="item" data-value="male" data-text="Male">
<i class="male icon"></i> Male
</div>
<div class="item" data-value="female" data-text="Female">
<i class="female icon"></i> Female
</div>
</div>
</div>
<script src="/lib/require.js"></script>
<script>
require.config({
paths: {
jquery: ['//cdn.bootcss.com/jquery/1.9.1/jquery.min', '/lib/jquery'],
semanticUi: '//cdn.staticfile.org/semantic-ui/2.2.7/semantic.min'
},
shim: {
semanticUi: {
deps: ['jquery']
}
}
});
</script>
<script>
require(['jquery', 'semanticUi'], function($) {
$('.dropdown').dropdown();
});
</script>
</body>
</html>