问题描述
我正在尝试将方法从基本窗口小部件继承到另一个窗口小部件.这是我的示例代码
I am trying to inherit a method from base widget to another widget.here is my sample code
我的基本小部件是
$(function () {
$.widget("UI.baseWidget", {
options: {
testVal:''
},
_create: function () {
alert(this.option.testVal);
},
});
});
和其他称为该基本小部件的小部件是
and other widget calling this base widget is
$(function () {
$.widget("UI.MyWidget", $.UI.baseWidget, {
options: {
testVal:''
},
_create: function () {
$.UI.baseWidget.prototype._create.call(this);
}
});
});
并初始化MyWidgetcode为'
and initilise the MyWidgetcode is'
$('#mydiv').MyWidget({testVal:'test widget'})
如何将MyVal的testVal选项传递给baseWidget调用?而我却收到类似
How can we pass testVal option from MyWidget to baseWidget call?and I getting error some thing like
未捕获的TypeError:我不是构造函数
Uncaught TypeError: i is not a constructor
未捕获的TypeError:$(...).MyWidget不是函数
Uncaught TypeError: $(...).MyWidget is not a function
可以帮助我解决此问题.预先感谢
can please help me to fix this issue. thanks in advance
推荐答案
似乎可以正常工作:我只作了一次更正:将选项更改为alert(this.option.testVal);
中的选项,并弹出带有测试小部件"的警报,确定.您也可以尝试创建小部件jquery onReady
,看看是否可以解决问题,即:
Seems to work OK: I only made one correction : changed option to options in alert(this.option.testVal);
and the alert with 'test widget' popped up OK. You can also try to create the widget jquery onReady
and see if that fixes the problem i.e. :
$(function () {
$('#myDiv').MyWidget({testVal:'test widget'});
});
在 https://jsfiddle.net/5gmn6x7k/4/
这篇关于如何在Jquery UI小部件中实现继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!