Dojo 學(xué)習(xí)筆記入門篇 First Dojo Example
更新時(shí)間:2009年11月15日 00:03:50 作者:
Dojo學(xué)習(xí)筆記入門篇,第一個(gè)小例子, 剛開始學(xué)習(xí)dojo的朋友可以參考下。
Step 1: Configurate Dojo
從http://www.dojotoolkit.org/downloads 下載最新的Dojo包,并且放到你工程的某一個(gè)地方。比如,我就放把我的dojo庫放在lib文件夾下(如圖一)。
圖一(文件目錄結(jié)構(gòu))
在你的頁面中加入如下代碼,那么就完成了最基本的配置。
<script type="text/javascript" src="lib/dojo/dojo/dojo.js" djConfig ="parseOnLoad:true, isDebug:false"></script>
Step 2: Start your First Example
我的第一個(gè)例子是模擬一個(gè)簡單的登錄頁面,如果成功,則顯示“Right!”,反之則顯示“Wrong!Please try it again!”
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript" src="lib/dojo/dojo/dojo.js" src="lib/dojo/dojo/dojo.js" djConfig ="parseOnLoad:true, isDebug:false"></script>
<style type="text/css"><!--
#container {
border: 1px dotted #b7b7b7;
background: #ededed;
width: 200px;
height: 200px;
}
--></style><style type="text/css" bogus="1"> #container {
border: 1px dotted #b7b7b7;
background: #ededed;
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<form id="mainForm" action="controller/ajax-controller.php" method="post">
<label for = "name">Name:</label>
<input type="text" name="name"/>
<br/>
<label for = "password">Password:</label>
<input type="password" name="password" value=""/>
<br/>
<input type="submit" value="Submit"/>
<h1 id="resultText"></h1>
</form>
<script type="text/javascript"><!--
var formSubmit = function(e){
e.preventDefault();
var resultText = dojo.byId("resultText");
dojo.xhrPost({
url: "controller/controller.php",
form:"mainForm",
handleAs:"text",
handle:function(data,args){
console.info(data);
if(typeof data=="error"){
resultText.innerHTML = "<font color=\"red\">error!</font>";
}else{
if(data === "right"){
resultText.innerHTML = "Right!";
}else if(data==="wrong"){
resultText.innerHTML = "<font color=\"red\">Wrong!Please try it again!</font>";
}
}
}
});
};
dojo.addOnLoad(function(){
var theForm = dojo.byId("mainForm");
dojo.connect(theForm,"onsubmit","formSubmit");
});
// --></script>
</body>
</html>
(對于其中的Dojo方法,可以查看Dojo的API,在此不再贅述)
后臺(tái)采用php,代碼如下:
<?php
if($_POST["name"] == "blithe" && $_POST["password"]=="blithe"){
print "right";
}else{
print "wrong";
}
?>
從http://www.dojotoolkit.org/downloads 下載最新的Dojo包,并且放到你工程的某一個(gè)地方。比如,我就放把我的dojo庫放在lib文件夾下(如圖一)。

圖一(文件目錄結(jié)構(gòu))
在你的頁面中加入如下代碼,那么就完成了最基本的配置。
<script type="text/javascript" src="lib/dojo/dojo/dojo.js" djConfig ="parseOnLoad:true, isDebug:false"></script>
Step 2: Start your First Example
我的第一個(gè)例子是模擬一個(gè)簡單的登錄頁面,如果成功,則顯示“Right!”,反之則顯示“Wrong!Please try it again!”
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript" src="lib/dojo/dojo/dojo.js" src="lib/dojo/dojo/dojo.js" djConfig ="parseOnLoad:true, isDebug:false"></script>
<style type="text/css"><!--
#container {
border: 1px dotted #b7b7b7;
background: #ededed;
width: 200px;
height: 200px;
}
--></style><style type="text/css" bogus="1"> #container {
border: 1px dotted #b7b7b7;
background: #ededed;
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<form id="mainForm" action="controller/ajax-controller.php" method="post">
<label for = "name">Name:</label>
<input type="text" name="name"/>
<br/>
<label for = "password">Password:</label>
<input type="password" name="password" value=""/>
<br/>
<input type="submit" value="Submit"/>
<h1 id="resultText"></h1>
</form>
<script type="text/javascript"><!--
var formSubmit = function(e){
e.preventDefault();
var resultText = dojo.byId("resultText");
dojo.xhrPost({
url: "controller/controller.php",
form:"mainForm",
handleAs:"text",
handle:function(data,args){
console.info(data);
if(typeof data=="error"){
resultText.innerHTML = "<font color=\"red\">error!</font>";
}else{
if(data === "right"){
resultText.innerHTML = "Right!";
}else if(data==="wrong"){
resultText.innerHTML = "<font color=\"red\">Wrong!Please try it again!</font>";
}
}
}
});
};
dojo.addOnLoad(function(){
var theForm = dojo.byId("mainForm");
dojo.connect(theForm,"onsubmit","formSubmit");
});
// --></script>
</body>
</html>
(對于其中的Dojo方法,可以查看Dojo的API,在此不再贅述)
后臺(tái)采用php,代碼如下:
復(fù)制代碼 代碼如下:
<?php
if($_POST["name"] == "blithe" && $_POST["password"]=="blithe"){
print "right";
}else{
print "wrong";
}
?>
相關(guān)文章
dojo學(xué)習(xí)第一天 Tab選項(xiàng)卡 實(shí)現(xiàn)
可能很多人都對dojo只聞其名,覺得有了jquery、prototype、YUI等這些優(yōu)秀的js庫了,dojo還有它存在的必要嗎?2011-08-08Dojo之路:如何利用Dojo實(shí)現(xiàn)Drag and Drop效果
Dojo之路:如何利用Dojo實(shí)現(xiàn)Drag and Drop效果...2007-04-04利用Dojo和JSON建立無限級AJAX動(dòng)態(tài)加載的功能模塊樹
2007-03-03dojo學(xué)習(xí)第二天 ajax異步請求之綁定列表
在上一篇《dojo學(xué)習(xí)第一天 Tab選項(xiàng)卡》,我們學(xué)到了,怎么用dojo的選項(xiàng)卡插件來制作更易于用戶使用的表單,所有的一切都是為了使用更加方便,一切都是為了用戶的體驗(yàn)2011-08-08