dojo.connect不会按钮连接的“onClick” [英] dojo.connect won't connect 'onclick' with button
问题描述
我遇到一个问题,使用dojo.connect()来连接这个按钮的的onclick事件:
I'm running into a problem using dojo.connect() to connect an 'onclick' event with this button:
<button dojoType="dijit.form.Button" widgetId="inbox_button" id="inbox_button">Inbox</button>
和code使得连接是:
and the code making the connection is:
var inbox_button=dojo.byId("inbox_button");
dojo.connect(inbox_button,'onclick',function(){
var container=dijit.byId("center");
container.addChild(new dijit.layout.ContentPane({region: "left", content: "...", style: "width: 100px;"}))
});
然而,而不是在单击按钮时执行的功能,任何onclick事件触发功能,我结束了许多孩子的容器。
However, instead of executing the function when the button is clicked, ANY onclick event triggers the function, and I end up with a lot of children containers.
虽然我pretty一定的.connect()函数应可作为Dojo的基本功能的一部分,我'需要'它明确:
Even though I'm pretty certain the .connect() function should be available as part of dojo's base functionality, I've 'required' it explicitly:
dojo.require("dojo._base.connect");
任何想法,为什么这可能发生?
Any ideas as to why this might be happening?
推荐答案
正如你所说,如果运行code前的DOM准备就绪,dojo.byId(inbox_button)将返回null。所以,你的连接实际上是这样做的:
As you say, if you run that code before the DOM is ready, dojo.byId("inbox_button") will return null. So your connect is actually doing:
dojo.connect(null, "onclick", function() { ... })
..如果第1个参数dojo.connect为空,全局或'窗口'对象将被使用。
.. if the 1st arg to dojo.connect is null, the global or 'window' object will be used.
不过,这是这里只是一个错误。您按钮元素越来越小部件,美化版,并变成了dijit.form.Button。所以,你应该连小部件的onClick方法在这里,而不是节点的onclick:
But, that's just one error here. Your button element is getting widget-ized, and turned into a dijit.form.Button. So you should be connecting to the widget's onClick method here, not the node's onclick:
dojo.connect(dijit.byId("inbox_button"), "onClick", function() { ... });
另外,要清楚,你永远不应该有dojo.require在dojo._base什么,就是这样的dojo.js的承诺让你 - 所有的捆绑
Also, to be clear, you should never have to dojo.require anything in dojo._base, that's the promise that dojo.js makes to you - its all bundled in.
这篇关于dojo.connect不会按钮连接的“onClick”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!