我怎样才能制作一个数据 dojo 附加事件 运行?
How can I make a data-dojo-attach-event run?
我正在尝试 运行 以编程方式添加 domNode 后的事件:
<a href="javascript:void(0)" data-dojo-attach-event="click:openRegistration">Register</a>
首次加载页面时,Dojo 不会解析此事件,因为稍后会添加该事件。即使在 运行ning
之后
parser.parse();
事件没有运行。我怎样才能举办这个活动运行?
您应该使用 onclick:openRegistration
而不是 click:openRegistration
。
<a href="javascript:void(0)" data-dojo-attach-event="onclick:openRegistration">Register</a>
没有看到你们的其余代码,我怀疑你们有范围问题。或者您没有正确设置 dom 事件 - onClick
您的函数需要属于使用继承 _TemplatedMixin 的 attach-events 模板的同一小部件。使用 attach-event 的小部件应该类似于
require([
'dojo/declare',
'dijit/_WidgetBase',
'dijit/_TemplatedMixin',
'dojo/text!myNameSpace/templates/MyWidget.html'
],
function(declare, _WidgetBase, _TemplatedMixin, template){
declare('MyWidget', [ _WidgetBase, _TemplatedMixin ], {
templateString: template,
openRegistration: function {
// do stuff here
}
});
});
您不希望 dojo 解析器解析页面两次:它会冗余地解析和创建已经创建的内容并导致混乱。要在页面解析后以编程方式添加节点,请查看 dojo/dom-construct.
require(["dojo/dom-construct", "dojo/domReady!"],
function(domConstruct) {
var openRegistration = function() {
alert("foo");
}
domConstruct.create("a", {
innerHTML: "Register",
click: openRegistration,
href: "javascript:void(0)"
}, document.body);
});
将document.body替换为对要插入节点的父节点的引用,并查看放置选项的第三个参数。
我正在尝试 运行 以编程方式添加 domNode 后的事件:
<a href="javascript:void(0)" data-dojo-attach-event="click:openRegistration">Register</a>
首次加载页面时,Dojo 不会解析此事件,因为稍后会添加该事件。即使在 运行ning
之后parser.parse();
事件没有运行。我怎样才能举办这个活动运行?
您应该使用 onclick:openRegistration
而不是 click:openRegistration
。
<a href="javascript:void(0)" data-dojo-attach-event="onclick:openRegistration">Register</a>
没有看到你们的其余代码,我怀疑你们有范围问题。或者您没有正确设置 dom 事件 - onClick
您的函数需要属于使用继承 _TemplatedMixin 的 attach-events 模板的同一小部件。使用 attach-event 的小部件应该类似于
require([
'dojo/declare',
'dijit/_WidgetBase',
'dijit/_TemplatedMixin',
'dojo/text!myNameSpace/templates/MyWidget.html'
],
function(declare, _WidgetBase, _TemplatedMixin, template){
declare('MyWidget', [ _WidgetBase, _TemplatedMixin ], {
templateString: template,
openRegistration: function {
// do stuff here
}
});
});
您不希望 dojo 解析器解析页面两次:它会冗余地解析和创建已经创建的内容并导致混乱。要在页面解析后以编程方式添加节点,请查看 dojo/dom-construct.
require(["dojo/dom-construct", "dojo/domReady!"],
function(domConstruct) {
var openRegistration = function() {
alert("foo");
}
domConstruct.create("a", {
innerHTML: "Register",
click: openRegistration,
href: "javascript:void(0)"
}, document.body);
});
将document.body替换为对要插入节点的父节点的引用,并查看放置选项的第三个参数。