将事件附加到 MooTools 中的类?
我对 MooTools 还很陌生,只是对如何使用它有点困惑,希望得到一些指导。
例如,在我的应用程序中,当初始化我的一个类的对象时,它会创建一个 div 元素。然后将该 div 放入另一个 div 中。用户可以创建该类的多个实例。我现在想知道如何向该对象添加事件,以便它在单击、双击等操作后可以做出反应。下面是我创建的类:
var new_Div = new Class({
initialize: function(name)
{
this.newDiv = name + "_div";
this.newDiv = document.createElement('div');
this.newDiv.id = this.classDiv;
this.newDiv.style.width = "200px";
this.newDiv.style.height = "160px";
this.newDiv.style.border = "thin red dashed";
document.body.appendChild(this.newDiv);
}
});
Div 由用户通过从 div id 的文本框中获取输入来创建和命名。然后使用此代码将生成的 div 插入到主体中,该代码调用initialize() 函数并创建一个 div:
var divName= document.getElementById("newdiv_Input").value;
window[divName+ '_var'] = new new_Div(divName);
这样就允许使用不同的名称创建相同类型的对象。
现在我感到困惑的是如何将事件附加到类中。例如,我如何创建一个事件,允许每个 div 被左键单击并运行一个函数,将事件附加到类的这个问题真的让我很困惑。有人可以帮我吗?
I am fairly new to MooTools and I'm just a little confused about how to use it and would like a few pointers.
For example, in my app, when an object of one of my classes is initialized, it creates a div element. This div is then placed into another div. The user may create multiple instances of the class. I would now like to know how to add events to this object so that it may react after being clicked, double-clicked and so-on. Below is the class I have created:
var new_Div = new Class({
initialize: function(name)
{
this.newDiv = name + "_div";
this.newDiv = document.createElement('div');
this.newDiv.id = this.classDiv;
this.newDiv.style.width = "200px";
this.newDiv.style.height = "160px";
this.newDiv.style.border = "thin red dashed";
document.body.appendChild(this.newDiv);
}
});
Divs are created and named by the user by taking there input from a textbox for the divs id. The generate div is then inserted into the body using this code which calls the initialize() function and creates a div:
var divName= document.getElementById("newdiv_Input").value;
window[divName+ '_var'] = new new_Div(divName);
This then allows for the same type of object to be created with different names.
Now the thing I am confused about is how to attach events to the class. For example, how would I create an event that allows each div to be left clicked and have a function run, this issue of attaching events to the class has really confused me. Could anyone help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上取决于您所说的“事件到类”的含义。您的意思是,让类向实例触发事件(通过事件混合),或者让元素向类触发事件/按类将事件附加到 dom 节点。考虑到这一点,下面的示例同时执行这两项操作:通过类内的 DOM 设置通用事件处理程序,并且让该类通知单击的实例,以便您可以覆盖实例实例化中的功能。
与您在另一个线程中的帖子相关:
这里它正在工作: http://jsfiddle.net/dimitar/cgDrG /
最后,考虑使用 toElement,它允许您处理类实例作为 dom 元素并直接导出您制作的 div - 工作示例,对于表单等很有用。
这是在 mootools 中使用的正确语法,您的行并不是很好的做法。另外,这是您几天来的第二个问题。考虑使您的帐户永久化并接受一些答案。
这是一个带有迷你验证器类的实际示例,该类检查必填字段并在不满足时触发错误框类的实例: http://jsfiddle.net/dimitar/cgDrG/5/
it really depends on what you mean by Events to Classes. You either mean, have the class fire up events to the instance (via the Events mixin) or have the Element fire events to the class / attach Events to dom nodes by Class. With that in mind, here's an example that does both: sets a generic event handler via DOM within the class and also has the class notify the instance of the click so you can override the functionality from your instance instantiation.
in relation to your post in the other thread:
here it is working: http://jsfiddle.net/dimitar/cgDrG/
and finally, consider using toElement, which allows you to treat the class instance as a dom element and export the div you make directly - working example, useful for forms etc.
this is the correct syntax to use in mootools, your lines are not exactly good practices. Also, this is your second question in as many days. consider making your account permanent and accepting some answers.
here's a practical example with a mini validator class that checks for required fields and fires off an instance of the error box class if not satisfied: http://jsfiddle.net/dimitar/cgDrG/5/
而不是给出详细的解释;我将添加到您的代码中,然后让您自然地从那里弄清楚它。你应该随意尝试一下。
“$()”是我指的元素选择器。你应该熟悉这一点。 Mootools 有很棒的文档,因此请花一些时间查看 这里的 Mootools 元素 和 此处介绍 mootools 事件。
Instead of giving a detailed explanation; I'll just add to your code and then let you naturally figure it out from there. You should feel free to mess with it and experiment.
"$()" is the element selector I was referring to. You should become familiar with this. Mootools has great documentation, so spend some time looking here on the Mootools element and here on mootools events.