繁体   English   中英

jQuery插件函数调用

[英]JQuery plugin function calling

我想从我的页面调用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);

该插件是Pine Steps向导。 它的代码是:

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

由于go_to_steps不是jQuery扩展(并且psteps()的返回值是原始jQuery对象psteps() ,因此在以下行中失败了:

pstps.go_to_step(step_num);

您需要实际插件的实例。 查看插件代码,它将实例作为属性连接到DOM元素pines_steps ,因此您需要将该属性作为类实例:

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);

但是此插件缺少执行此操作的代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM