开发者

DOJO Event Handler not working

red a I have registered a Event Handler With a HTML Component , but its not working , could anybody please help me on this :

<html>
  <head>
   <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/dojo.xd.js" type="text/javascript"></script>
   <script type="text/javascript"&开发者_StackOverflow社区gt;
        dojo.require("dojo.event.*");
        function openAlert(evt) {
        alert("Hello!  This is an alert from Dojo!");
        }
    </script>
  </head>
  <body>
    <p id="para">This is a paragraph.</p>
      <script type="text/javascript">
      var para = dojo.byId("para");
      dojo.event.connect(para, "onclick", openAlert);
      </script>
   </body>
</html>

Thank You .


it would be better if you add on click handler in dojoAddOnLoad.

Code:

function onClickHandler(event) {
    alert('clicked!')
}

    dojo.addOnLoad(function(){
        dojo.connect(dojo.byId('para'), 'onclick', onClickHandler);
    });

Your working example


First, dojo.require("dojo.event.*"); is not correct. I don't think you can load wild-cards to dojo.require. Also dojo.event appears to be deprecated: it is only dojo.connect now.

You are loading from CDN (xd = cross-domain). It is an "async" load -- which means that when your code is run, the Dojo library may not be loaded yet.

You need to put your user code into dojo.addOnLoad or the newer style dojo.ready. These are called after all Dojo modules are downloaded.


Use dojo.connect instead. You just need to import dojo, no extra packages, and change dojo.event.connect to dojo.connect; The rest of your code stays as is, see below.

<html>
  <head>
   <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/dojo.xd.js" type="text/javascript"></script>
   <script type="text/javascript">
        function openAlert(evt) {
          alert("Hello!  This is an alert from Dojo!");
        }
    </script>
  </head>
  <body>
    <p id="para">This is a paragraph.</p>
      <script type="text/javascript">
        var para = dojo.byId("para");
        dojo.connect(para, "onclick", openAlert);
      </script>
  </body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜