1. The HTML file

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
</head>
<body>
    <input type="button" id="button1" value="Click">
    <input type="button" id="button2" value="Click">
    <script src="https://cdn.bootcss.com/require.js/2.3.5/require.js" data-main="js/index"></script>
</body>
</html>
Copy the code

2. The index.js file corresponding to data-main in the HTML file above

require.config({
    baseUrl: 'js'
});

require(['myModule'].function (myModule){$('#button1').click(function () {
        alert('button1');
    });

    myModule.myFun();
});
Copy the code

3. The index.js file above corresponds to the mymodue.js file

define(function (){
    var myModule = {
        myFun: function () {$('#button2').click(function () {
                alert('button2'); }); }};return myModule;
});
Copy the code

4. Directory structure

\

\