在 javascript 对象中动态分配事件
我有以下 javascript 类:
var ImageGallery = function(arr) {
var ImgArr = arr;
var MainImgId = "";
this.build = function() {
var div = document.createElement("div");
for (var i = 0; i < ImgArr.length; i++) {
var img = document.createElement("img");
img.scr = ImgArr[i];
// what to do next, how to assign loadMainImage function ?????
img.onclick =
div.appendChild(img);
}
}
this.loadMainImage = function(imgPath) {
document.getElementById(MainImgId).src = imgPath;
}
}
接下来,在代码中我创建该类的对象:
var gallery = new ImageGallery(someArrWithPaths);
gallery.MainImgId = 'idOfMainImg';
gallery.build();
如何在每次单击图像时分配 img.onclick 事件来启动 loadMainImage(imgPath) 函数?
多谢!
I have following javascript class:
var ImageGallery = function(arr) {
var ImgArr = arr;
var MainImgId = "";
this.build = function() {
var div = document.createElement("div");
for (var i = 0; i < ImgArr.length; i++) {
var img = document.createElement("img");
img.scr = ImgArr[i];
// what to do next, how to assign loadMainImage function ?????
img.onclick =
div.appendChild(img);
}
}
this.loadMainImage = function(imgPath) {
document.getElementById(MainImgId).src = imgPath;
}
}
next, in code I create an object of that class:
var gallery = new ImageGallery(someArrWithPaths);
gallery.MainImgId = 'idOfMainImg';
gallery.build();
How can I assign img.onclick event to launch loadMainImage(imgPath) function every time I click on the image?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要捕获
this
的当前值(指的是ImageGallery
对象),并在onclickloadMainImage
。代码>.在onclick
中,this
指的是图像本身,因此this.src
将为您提供图像源:You need to capture the current value of
this
(which refers to theImageGallery
object) and invokeloadMainImage
on it withinonclick
. Insideonclick
,this
refers to the image itself, sothis.src
will give you the image source:我会尽量避免使用像 onevent 这样的方法(在你的例子中是 onclick),因为很容易失去这个处理程序(只需在其他地方编写 img.onclick 就会覆盖以前的处理程序)
尝试使用一些 javascript 库(如 jQuery),它会至少简化你的开发。回到你的问题,使用 YUI 2 我会这样写:
你可以在 jQuery 中找到类似的函数或其他框架。它的作用:
YAHOO.util.Event.addListener(obj, event, handler, argument, context)
- 将事件侦听器附加到obj
,用于event
和 <代码>处理程序指定。argument
- 传递给该处理程序的内容,以及执行该处理程序的context
(在您的情况下,它只是this
)当您卸载页面(或执行任何操作)时,不要忘记取消附加此处理程序,例如
YAHOO.util.Event.removeListener(img, "click", this.loadMainImage)
或调用以下函数:删除所有附加的侦听器(如 YUI 的purgeElement
)。是的,当您使用img.onclick = ...;
时,事情会更容易一些,似乎在这种情况下浏览器会处理它。I would try to avoid using methods like onevent (in your case onclick), because it is easy to loose this handler (just writing img.onclick somewhere else will override previous handler)
try to use some javascript library (like jQuery), it will simplify your development at least a bit. And back to your question, using YUI 2 I would write so:
You can find similar function in jQuery or other frameworks. What it does:
YAHOO.util.Event.addListener(obj, event, handler, argument, context)
- attach event listener toobj
, forevent
andhandler
specified.argument
- what to pass to that handler, andcontext
to execute that handler (in your case it will be justthis
)Doing this just don't forget to unattach this handler when you unload your page (or whatever you do), e.g.
YAHOO.util.Event.removeListener(img, "click", this.loadMainImage)
, or calling function which removes all listener attached (like YUI'spurgeElement
). Yes, things are a bit easier when you useimg.onclick = ...;
, seems like in this case browser will take care about it.