Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery插件获取和设置消息_Javascript_Jquery - Fatal编程技术网

Javascript jquery插件获取和设置消息

Javascript jquery插件获取和设置消息,javascript,jquery,Javascript,Jquery,我需要写一个插件,可以用类似的方式使用:这不是我真正想用它,但这是我的测试开始,以获得全面的理解 Html: 我缺少的是在元素上创建插件实例的能力,并使用它来获取和设置属性: 我假设我的设置有点像下面,有人能填补空白吗?例如,我不知道如何调用“get”方法以及如何通过传递参数 $.fn.plugin = function () { var target = this; methods: { var message = { get: fu

我需要写一个插件,可以用类似的方式使用:这不是我真正想用它,但这是我的测试开始,以获得全面的理解

Html:

我缺少的是在元素上创建插件实例的能力,并使用它来获取和设置属性:

我假设我的设置有点像下面,有人能填补空白吗?例如,我不知道如何调用“get”方法以及如何通过传递参数

$.fn.plugin = function () {
    var target = this;

    methods: {

        var message = {
            get: function () {
                return $(target).text();
            },
            set: function () {
                $(target).text(message);
            },
        };

    };

};

这是jQuery插件常用的基本框架:

(function($)
{
    var parameters = 
    {
        myDefault: "default"
    };

    var methods = {
        init : function(options) {
            return this.each(function() {
                parameters = $.extend(parameters, options);
            });
        },
        myMethod: function(){}
    };

    $.fn.myPlugin = function(methodOrOptions) {
        if ( methods[methodOrOptions] ) {
            return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
            // Default to "init"
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.myPlugin' );
        }    
    };

})(jQuery);
这样使用它:

init

$('#myDiv').myPlugin({
    myDefault: "override"
});
这将调用
init
方法,在本例中,该方法将更改默认参数

调用方法

$('#myDiv').myPlugin('myMethod'); 
请参阅此处的更多信息:

$('#myDiv').myPlugin({
    myDefault: "override"
});
$('#myDiv').myPlugin('myMethod');