在 javascript 对象中动态分配事件

发布于 2024-10-12 18:43:37 字数 849 浏览 1 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋竹姑娘 2024-10-19 18:43:37

您需要捕获 this 的当前值(指的是 ImageGallery 对象),并在 onclickloadMainImage 。代码>.在 onclick 中,this 指的是图像本身,因此 this.src 将为您提供图像源:

var self = this;
img.onclick = function() {
  self.loadMainImage(this.src);
};

You need to capture the current value of this (which refers to the ImageGallery object) and invoke loadMainImage on it within onclick. Inside onclick, this refers to the image itself, so this.src will give you the image source:

var self = this;
img.onclick = function() {
  self.loadMainImage(this.src);
};
忆伤 2024-10-19 18:43:37

我会尽量避免使用像 onevent 这样的方法(在你的例子中是 onclick),因为很容易失去这个处理程序(只需在其他地方编写 img.onclick 就会覆盖以前的处理程序)

尝试使用一些 javascript 库(如 jQuery),它会至少简化你的开发。回到你的问题,使用 YUI 2 我会这样写:

var imgCount = ImgArr.length; // do not use ImgArr.length in for definition, it is better 
// to save it once and then use... otherwise each time in loop will look up ImgArr object to find length property
for (var i = 0; i < imgCount; i++) {
     var img = document.createElement("img");
     var src = ImgArr[i];
     img.scr = src ;
     YAHOO.util.Event.addListener(img, "click", this.loadMainImage, src, this);
}

你可以在 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:

var imgCount = ImgArr.length; // do not use ImgArr.length in for definition, it is better 
// to save it once and then use... otherwise each time in loop will look up ImgArr object to find length property
for (var i = 0; i < imgCount; i++) {
     var img = document.createElement("img");
     var src = ImgArr[i];
     img.scr = src ;
     YAHOO.util.Event.addListener(img, "click", this.loadMainImage, src, this);
}

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 to obj, for event and handler specified. argument - what to pass to that handler, and context to execute that handler (in your case it will be just this)

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's purgeElement). Yes, things are a bit easier when you use img.onclick = ...;, seems like in this case browser will take care about it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文