观察“点击”; 上的事件由 scriptaculous Builder 生成的标签
我正在使用 scriptaculous Builder 动态生成一些 DOM 元素,其中之一是链接标签。我不确定如何使用内联的单击回调以及生成器代码的其余部分来生成此内容,因此我提前生成链接标记,然后将其与生成器阶段的其余部分一起插入。
问题是,当我单击链接时,链接的回调从未真正执行,并且 URL 栏更改为 http而是使用://localhost/foo/bar/#。回调方法是我的类的一部分,因此我提前将其绑定为事件侦听器。
var Foo = Class.create ({
initialize: function () {
this.closeBinding = this.doClose.bindAsEventListener (this);
},
generate: function () {
/* Create the link and bind the click listener */
var close_link = Builder.node ('a', { href: '#' }, 'Close');
Event.observe (close_link, 'click', this.closeBinding);
/* Generate the new DOM nodes */
return Builder.node ('div', [
Builder.node ('h2', 'This is a test'),
close_link
]);
},
doClose: function (evt) {
/* This code is never called when I click the link. */
}
});
I'm using scriptaculous Builder to generate some DOM elements dynamically, and one of them is a link tag. I wasn't sure how to generate this with the click callback inline along with the rest of the Builder code, so I'm generating the link tag ahead of time and then inserting it with the rest of the Builder phase.
The problem is that the callback for the link is never actually executed when I click the link, and the URL bar changes to http://localhost/foo/bar/# instead. The callback method is a part of my class, so I'm binding it as an event listener ahead of time.
var Foo = Class.create ({
initialize: function () {
this.closeBinding = this.doClose.bindAsEventListener (this);
},
generate: function () {
/* Create the link and bind the click listener */
var close_link = Builder.node ('a', { href: '#' }, 'Close');
Event.observe (close_link, 'click', this.closeBinding);
/* Generate the new DOM nodes */
return Builder.node ('div', [
Builder.node ('h2', 'This is a test'),
close_link
]);
},
doClose: function (evt) {
/* This code is never called when I click the link. */
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以,我发现了真正的问题。实际上,就我观察事件的方式而言,我并没有做任何错误,但我遗漏了一个看似不相关但显然相关的细节。我生成的 DOM 代码被插入到 Modalbox[1] 中,显然该脚本正在干扰我的事件绑定或其他内容。观察“this”上的函数的链接很困惑,因为显然“this”指的是 Modalbox,而不是我的对象 Foo。
所以我的解决方案可能不太理想,但我找到了另一种方法来访问“this”中的数据,因此事件绑定现在不妨碍我了。
[1]。 http://okonet.ru/projects/modalbox/
So, I discovered the real problem. I actually wasn't doing anything wrong in terms of how I was observing the event, but I left out a detail that didn't seem relevant but apparently was relevant. The DOM code I'm generating is something that is being inserted into a Modalbox[1], and apparently that script is interfering with my event binding or something. The link that is observing a function on 'this' was confused because apparently 'this' was referring to the Modalbox, and not to my object Foo.
So my solution was perhaps a little less than ideal, but I found another way to access the data that was in 'this', and so the event binding is not in my way now.
[1]. http://okonet.ru/projects/modalbox/