dojo1.9学习总结(三)-事件绑定

前端之家收集整理的这篇文章主要介绍了dojo1.9学习总结(三)-事件绑定前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%
  4. String path = request.getContextPath();
  5. // 获得项目完全路径(假设你的项目叫MyApp,那么获得到的地址就是 http://localhost:8080/MyApp/):
  6. String basePath = request.getScheme() + "://"
  7. + request.getServerName() + ":" + request.getServerPort()
  8. + path + "/";
  9. %>
  10. <html>
  11. <head>
  12. <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  13.  
  14. <link rel="stylesheet" type="text/css"
  15. href="<%=basePath%>dojo/dijit/themes/soria/soria.css">
  16. <link rel="stylesheet" type="text/css"
  17. href="<%=basePath%>dojo/dojo/resources/dojo.css" />
  18. <link rel="stylesheet" type="text/css"
  19. href="<%=basePath%>dojo/dojox/grid/resources/soriaGrid.css" />
  20. <style type="text/css">
  21. body,html {
  22. font-family: helvetica,arial,sans-serif;
  23. font-size: 90%;
  24. }
  25.  
  26. th {
  27. font-size: 13px;
  28. font-family: Arial,Microsoft YaHei,SimSun,sans-serif !important;
  29. text-align: center;
  30. }
  31.  
  32. td {
  33. font-size: 13px;
  34. font-family: Arial,sans-serif !important;
  35. }
  36.  
  37. div #copyright {
  38. text-align: center
  39. }
  40. </style>
  41. </head>
  42. <script type="text/javascript" src="<%=basePath%>dojo/dojo/dojo.js"
  43. djConfig="parSEOnLoad:true">
  44. </script>
  45. <script type="text/javascript">
  46. dojo.require("dijit.layout.ContentPane");
  47. dojo.require("dijit.layout.BorderContainer");
  48. dojo.require("dijit.layout.AccordionContainer");
  49. dojo.require("dijit.layout.TabContainer");
  50. dojo.require("dijit.form.Button");
  51. dojo.require("dijit.Dialog");
  52. dojo.require("dojo.parser");
  53. </script>
  54. <script type="text/javascript">
  55. //利用dojo.connect和dojo.disconnect来进行事件绑定
  56. var handler = null;
  57. function connect() {
  58. var btn1 = dojo.byId("btn1");
  59. var btn2 = dojo.byId("btn2");
  60. var span = dojo.byId("span1");
  61. handler = dojo.connect(btn1,"onclick",function(evt) {
  62. dojo.style(span,"backgroundColor","green");
  63. });
  64. }
  65. function disconnect() {
  66. var span = dojo.byId("span1");
  67. dojo.disconnect(handler);
  68. dojo.style(span,"blue");
  69. }
  70. //还可以这样用dojo.connect
  71. function connect1() {
  72. var span = dojo.byId("span1");
  73. //这里只能使用dojo.query,使用dojo.byId("btn1")会出错
  74. handler = dojo.query("#btn1").connect("onclick","red");
  75. });
  76. }
  77. //可以利用dojo.stopEvent()和evt.preventDefault()来阻止浏览器处理DOM事件
  78. //还可以connect一个var对象中的事件
  79. function connect2() {
  80. var btn1 = dojo.byId("btn1");
  81. var btn2 = dojo.byId("btn2");
  82. var myObject = {
  83. id : "myObject",onClick : function(evt) {
  84. alert("the scope of this handler is " + this.id);
  85. }
  86. };
  87. dojo.connect(btn1,myObject.onClick);
  88. //注意这里的第三个参数myObject,表明了绑定事件的context(上下文),默认情况下不用此参数
  89. dojo.connect(btn2,myObject,myObject.onClick);
  90. }
  91.  
  92. //publish/subscribe
  93. function subscribe() {
  94. //直接订阅信息,但不针对某个具体的目标
  95. handler = dojo.subscribe("hi",function(evt) {
  96. alert(evt);
  97. });
  98. }
  99. function alertUser() {
  100. var text = [ "i am alert you" ];
  101. //直接发布消息,但不针对某个具体的目标
  102. dojo.publish("hi",text);
  103. dojo.unsubscribe(handler);
  104. }
  105. function anotherAlert() {
  106. var text = [ "i am another alert you" ];
  107. //直接发布消息,但不针对某个具体的目标
  108. dojo.publish("hi",text);
  109. }
  110. dojo.ready(subscribe);
  111. </script>
  112. <body class="soria">
  113. <div id="container">
  114. <button id="btn1" data-dojo-type="dijit.form.Button">click me</button>
  115. <button id="btn2" data-dojo-type="dijit.form.Button"
  116. data-dojo-props="onClick:disconnect">disconnect</button>
  117. <span id="span1">this is text</span> <br />
  118. <button id="alertButton" data-dojo-type="dijit.form.Button"
  119. data-dojo-props="onClick:alertUser">alert the user</button>
  120. <button id="createAlert" data-dojo-type="dijit.form.Button"
  121. data-dojo-props="onClick:anotherAlert">another</button>
  122. </div>
  123. </body>
  124. </html>

效果

猜你在找的Dojo相关文章