[英]Cannot connect click eventhandler to button in dojo
I am creating a small webservice, which needs a button to recenter the map to a predefined point. 我正在创建一个小型Web服务,它需要一个按钮来将地图更新到预定点。 Which seemed to be a one-minute requirement has kept me busy for the last 2 hours.
在过去的2个小时里,这似乎是一分钟的要求,这让我很忙。
The script is using ArcGis Js api 3.9. 该脚本使用的是ArcGis Js api 3.9。
This is the code in question: 这是有问题的代码:
//ClickEvent To reCenter the map
var ReCenterButton = dom.byId('btnReCenter');
ReCenterButton.on('click', function (e) {
map.centerAndZoom(new Point(14, 51), 9);
});
Markup: 标记:
<button id="btnReCenter" data-dojo-type="dijit/form/Button" type="button">ReCenter</button>
The line ReCenterButton = dom.byId(/* { ... } */);
行
ReCenterButton = dom.byId(/* { ... } */);
throws the error: 引发错误:
Uncaught TypeError: undefined is not a function.
未捕获的TypeError:未定义不是函数。
What am I missing here? 我在这里想念什么?
I think there are several errors here, first of all there is no Dojo 3.9, I suppose this is a typo and you mean Dojo 1.9. 我认为这里有几个错误,首先没有Dojo 3.9,我想这是一个错字,您的意思是Dojo 1.9。
The error you got means it cannot find a function, probably because you didn't import the dojo/dom
module and thus it cannot find the dojo/dom::byId()
function. 您得到的错误意味着它找不到函数,可能是因为您没有导入
dojo/dom
模块,因此它找不到了dojo/dom::byId()
函数。 Make sure the module is properly included in your require()
and it is mapped upon a variable called dom
, for example: 确保模块正确包含在
require()
,并且已映射到名为dom
的变量上,例如:
require([ "dojo/dom" ], function(dom) {
var ReCenterButton = dom.byId("btnReCenter");
});
However, a DOM node has no on()
function, so this could be the reason of the error as well. 但是,DOM节点没有
on()
函数,因此这也可能是错误的原因。 When you're using dojo/dom::byId()
it will return a plain DOM node. 当您使用
dojo/dom::byId()
,它将返回一个普通的DOM节点。 However, if you're working with widgets like dijit/form/Button
you should rather use the dijit/registry::byId()
function, for example: 但是,如果您正在使用
dijit/form/Button
类的小部件,则应使用dijit/registry::byId()
函数,例如:
require([ "dijit/registry", "dojo/ready", "dijit/form/Button", "dojo/parser" ], function(registry, ready) {
ready(function() { // Wait until the HTML markup is parsed and the widgets are instantiated
var ReCenterButton = registry.byId('btnReCenter'); // Use dijit/registry
ReCenterButton.on('click', function (e) {
map.centerAndZoom(new Point(14, 51), 9);
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.