我想从我的页面调用jquery插件函数,但失败了:我的代码是:
var pstps=$('#psteps_simple_horiz_layout').psteps({
steps_width_percentage: true,
alter_width_at_viewport: '1300',
steps_height_equalize: true
});
step_num=2;
pstps.go_to_step(step_num);插件是松树步骤向导。它的代码是:
function($) {
$.fn.psteps = function(options) {
// Build main options before element iteration.
var opts = $.extend({}, $.fn.psteps.defaults, options);
// Iterate and transform each matched element.
var all_elements = this;
all_elements.each(function(){
var psteps = $(this);
psteps.psteps_version = "0.0.1alpha";
.......................................................
psteps.go_to_step = function(step_num){
var last_active_title = psteps.find('.step-title.last-active'),
.......................................................
};
.......................................................
this.pines_steps = psteps;
});
return all_elements;
};当我运行我的代码时,我会得到错误:
Uncaught TypeError: undefined is not a function发布于 2015-02-18 09:44:07
由于go_to_steps不是jQuery扩展(来自psteps()的返回值是原始的jQuery对象),所以它在下面的行上失败了:
pstps.go_to_step(step_num);您需要实际插件的实例。查看插件代码,它将实例作为名为pines_steps的DOM元素上的属性连接,因此需要将该属性作为类实例:
var pstps=$('#psteps_simple_horiz_layout').psteps({
steps_width_percentage: true,
alter_width_at_viewport: '1300',
steps_height_equalize: true
})[0].pines_steps;然后你可以打电话
pstps.go_to_step(step_num);通用模式:
编写插件的通常方法是在第一个参数中接受函数名(作为字符串),这样它们就可以调用如下方法:
$('#psteps_simple_horiz_layout').psteps("go_to_step", step_num);然而,这个插件缺少这样做的代码。
https://stackoverflow.com/questions/28580263
复制相似问题