微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

dojo -- it is critical to pull in "dojo/domReady!"

Issue: Can not get handler of an element by using dojo.byId(“element id”) ;

You want to add an event to a button,code goes like this:

<script>

require(["dojo","dojo/on","dijit/registry","dojo/dom","dojox/mobile","dojox/mobile/parser","dojox/mobile/SwapView","dojox/mobile/PageIndicator","dojox/mobile/heading","dojox/mobile/ScrollableView","dojox/mobile/EdgetoEdgeList","dojox/mobile/Button”

],function(dojo,on,registry,dom){

     var handler = dojo.byId("btn");

     on(handler,"click",function(e){

           alert("i am clicked");

     });

});

</script>

     <script type="text/javascript" src="engmain.js"></script>

 

</head>

<body style="visibility:hidden">

     <div id="mainview" class="mainview" data-dojo-type="dojox/mobile/ScrollableView">

    

     <button id="btn" data-dojo-type="dojox/mobile/Button">click me

           <img src="images/bottomarrow.png"/>

     </button>

     <div id="result" ></div>

                maincontent

     </div>

</body>

No matter how hard you clicked the button,the button just did not give you any response.

And you will see this in your debugging environment (FireBug) – target is null

Possible Cause:

DOM tree didn't ready when dojo.byId(“elementID” executed,so the element you specified cannot be identified.

Reference: http://www.jetwu.cn/archives/101


Solution:

Ensure that you pull in dojo/domready! when you need to do something with element of the DOM tree.


The code is as below,for your reference.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

	<title>slip view</title>
	<link rel="stylesheet" href="">
	<script type="text/javascript" src="dojox/mobile/deviceTheme.js"></script>

	<script type="text/javascript" src="dojo/dojo.js" 
    	data-dojo-config="isDebug:false,parSEOnLoad: true,debugAtAllCosts:false"></script>

<script>
require(["dojo","dojox/mobile/Button","dojo/domready!"
],dom){
	var handler = dojo.byId("btn");	
	on(handler,function(e){
		alert("i am clicked");
	});
});

</script>
	<script type="text/javascript" src="engmain.js"></script>

</head>
<body style="visibility:hidden">
	<div id="mainview" class="mainview" data-dojo-type="dojox/mobile/ScrollableView">
	
		<button id="btn" data-dojo-type="dojox/mobile/Button">click me
		<img src="images/bottomarrow.png"/>
		</button>
	<div id="result" ></div>
			main content
	</div>
</body>


</html>

原文地址:https://www.jb51.cc/dojo/291181.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐