dojo的头部标准写法: style type="text/css"@import "dojo-lib/dojo/resources/dojo.css";@import "dojo-lib/dijit/themes/tundra/tundra.css";/stylescript type="text/javascript" djConfig="parseOnLoad: true, isDebug: false" src="dojo-lib/dojo
dojo的头部标准写法:
<style type="text/css"> @import "dojo-lib/dojo/resources/dojo.css"; @import "dojo-lib/dijit/themes/tundra/tundra.css"; </style> <script type="text/javascript" djConfig="parseOnLoad: true, isDebug: false" src="dojo-lib/dojo/dojo.js"></script>
数组的遍历:dojo.forEach
var array=[1,2,3,4,5]; dojo.forEach(array,function(x) { alert(x); });
dojo对象的创建:dojo.declare(对象名,父类类名,{函数体})。继承直接将第二个参数设置为父类的类名即可。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>测试</title> <style type="text/css"> @import "dojo-lib-1.9.3/dojo/resources/dojo.css"; @import "dojo-lib-1.9.3/dijit/themes/tundra/tundra.css"; </style> <script type="text/javascript" djConfig="parseOnLoad: true, isDebug: false" src="dojo-lib-1.9.3/dojo/dojo.js"></script> <script type="text/javascript"> dojo.declare("Person",null, { constructor:function(name,age) { this.name = name; this.age=age; }, introduce:function() { alert(this.name+"今年"+this.age+"岁"); }, }); var person = new Person("许增强",22); person.introduce(); </script> </head> <body> </body> </html>
对象的复制:
<script type="text/javascript"> dojo.declare("Person",null, { constructor:function(name,age) { this.name = name; this.age=age; }, introduce:function() { alert(this.name+"今年"+this.age+"岁"); }, }); var person = new Person("许增强",22); /* var copyPerson=person; //方式1.直接复制,只是给了person另外一个名字,属于浅复制 copyPerson.introduce(); var copyPerson=dojo.mixin({},person); //使用dojo.mixin()实现对象的复制 copyPerson.introduce(); */ var copyPerson=dojo.clone(person); //使用dojo.clone()实现对象的复制,确保完整的创建了新对象,属于深复制 copyPerson.introduce(); </script>
阻止事件冒泡和默认事件
dojo.stopEvent(event); //相当于同时调用了 event.stopPropagation(); //阻止事件冒泡 event.preventDefault(); //阻止默认事件
关联处理函数
dojo.connect(obj,event,context,handler);其中obj为DOM对象,event为事件名称,是一个字符串,context为可选参数,handler为事件处理函数,可以为一个函数或字符串。
dojo1.9以后的版本
新建一个dojo.html,dojo的目录结构为dojo-dojo-dojo.js,第一个dojo与dojo.html目录同级。在第一个dojo文件夹下新建一个framework文件夹,然后新建一个Person.js文件。
define(["dojo/_base/declare"],function(declare){ return declare("Person", null, { constructor:function(name,age) { this.name=name; this.age=age; }, say:function() { alert("My name is "+this.name); } }); });dojo.html代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>测试用例</title> <style type="text/css"> @import "dojo/dojo/resources/dojo.css"; @import "dojo/dijit/themes/tundra/tundra.css"; </style> <script type="text/javascript" djConfig="parseOnLoad: true, isDebug: false" src="dojo/dojo/dojo.js"></script> </head> <script type="text/javascript"> require(["framework/Person"],function(Person) { var person=new Person("xuzengqiang",12); person.say(); }); </script> <body class="tundra"> </body> </html>即可运行。