如何读取jQuery之外的类属性?

时间:2014-02-14 15:54:12

标签: javascript jquery oop

我写了一个JS类,我想使用jQuery的API,但不知道如何读取jQuery对象之外的值。

function Slideshow( originalslideshowid, nextslideshowId ) {

    this.originalslideshowId = originalslideshowid;
    this.nextslideshowId = nextslideshowId;

    $("#nextslideshow").click( function(e) {

        hideoriginal( this.originalslideshowId );

    } );

    this.showOriginal = function() {

        $( this.originalslideshow ).click(function(e) {

            shownext(this.nextslideshowId);

        });

    };

};

你可以猜到,this.originalslideshowId和this.nextslideshowId返回undefined,因为他们试图从jQuery对象中读取属性。如何让他们从幻灯片课程中读取正确的属性?

1 个答案:

答案 0 :(得分:1)

this保存到变量:

function Slideshow( originalslideshowid, nextslideshowId ) {
    var _this = this;

    this.originalslideshowId = originalslideshowid;
    this.nextslideshowId = nextslideshowId;

    $("#nextslideshow").click( function(e) {    
        hideoriginal( _this.originalslideshowId );    
    });

    this.showOriginal = function() {    
        $( this.originalslideshow ).click(function(e) {    
            shownext(_this.nextslideshowId);    
        });    
    };    
}