原文地址:http://6yang.net/articles_view.php?id=1103

2011-10-18 13:12:01 by 【6yang】, 1029 visits, 收藏 | 返回

例項說明:

DEMO[require]:http://6yang.net/myjavascriptlib/requirejs/demo/d1.html

DEMO[require-define]:http://6yang.net/myjavascriptlib/requirejs/demo/d2.html

DEMO[require-config]: http://6yang.net/myjavascriptlib/requirejs/demo/d3.html

說明下require,require-define,require-config,app-bulid.js(介紹)

require: 主要任務包括非同步載入事件及js庫。

define: 主要任務定義事件方便呼叫。

require.config(): 主要任務配置相關引數。

app-bulid.js: 建立app-build.js為是了nodejs建立專案使用的,分配目錄。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>requirejs 應用</title>
</head>
<style>
</style>
<body>

<!--<script src="js/jquery-1.6.2.min.js"></script>-->
<script src="js/require.js"></script>
<script>
define("Employee", function () {
    //You can name this function here,
    //which can help in debuggers but
    //has no impact on the module name.
    return function Employee(first, last) {
        this.first = first; 
        this.last = last;
    };
});
/*
define("main", ["Employee"], function (Employee) {
    var john = new Employee("John", "Smith");
    //alert(john.first);
});*/

/*  
 @ 這時的require包括了define裡定義的物件變數及載入js檔案物件.
 @ function(Employee)裡的Employee,是需要通過define裡的變數傳遞過來形參;
*/

require(["Employee","js/test","http://code.jquery.com/jquery-1.6.4.min.js"], function(Employee) {
    var john = new Employee("John", "Smith");
     alert(john.first)
    alert(objstu.country);
    var inputVal = $("input:checkbox").prop("name");
    alert(inputVal);
});

</script>

<div id="dic">sssssssssssss</div>
使用者名稱:<input type="text" name="username" value="1244555" /><br/>
性別: <input type="checkbox" name="sex" value="0" /> 
</body>
</html>

分享到:0