微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 扩展jquery ui小部件 – 如何访问父级事件

我正在尝试创建一个ui.slider开始的jQuery小部件.我想要一个在’slide’事件上执行的自定义方法.我尝试覆盖父选项,就像正常使用滑块小部件一样,但我遇到了变量范围的问题:
$.widget( "ui.myslider",$.ui.slider,{
    _create: function() {
        this.foo = "bar";

        // Outputs "bar"
        this._mySlide();

        // Outputs "undefined" when triggered
        this.options.slide = this._mySlide;

        $.ui.slider.prototype._create.apply(this,arguments);
    },_mySlide: function() {
        alert(this.foo);
    }
}

如何在幻灯片事件上触发我的功能并访问我的变量?

编辑:
链接到ui.slider来源:https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.slider.js

编辑:解决方

$.widget( "ui.myslider",{
    _create: function() {
        $.ui.slider.prototype._create.apply(this,arguments);
        this.foo = "bar";
    },_slide: function() {
        $.ui.slider.prototype._slide.apply(this,arguments);
        alert(this.foo);
    }
}

解决方法

在jQuery UI> = 1.9中,您可以使用_super方法
_slide: function() {
  this._super();
  // equivalent to $.Widget.prototype._slide.call( this );
}

或superApply:

_slide: function() {
  this._superApply(arguments);
  // equivalent to $.Widget.prototype._slide.apply( this,arguments );
}

原文地址:https://www.jb51.cc/jquery/150389.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐