English 中文(简体)
风俗杂
原标题:call methods in custom jquery plugin

我试图打造我的第一个gin。 至今:

(function( $ ){

  var settings = {
    key1:  t1 ,
    key2:  t2 
  };

  var methods = {
    init : function( options ) {

      return this.each(function() {
        var $this = $(this);

        console.log( init called );
        data = $this.data( snake );

        // If the plugin hasn t been initialized yet
        if (!data) {

          //Do setup stuff here

          $this.data( snake , {
            map: $this.find(".map"),
            stats: $this.find(".stats")
          });

          data = $this.data( snake );
        }

        if ( options ) { 
          $.extend( settings, options );
        }

        // HERE I WOULD LIKE TO CALL THE RUN METHOD AND BE ABLE TO USE settings AND data VARIABLES

      });

    },
    run : function( ) {

      var $this = $(this), data = $this.data( snake );
      console.log( run called );
      //test for settings and data
      console.log(settings);
      console.log(data);

    },
    test : function( ) {

      return this.each(function() {
        var $this = $(this), data = $this.data( snake );
        console.log( test called );
        //test for settings and data
        console.log(settings);
        console.log(data);
      });

    },
    setup : function (  ) {
      console.log( setup called );
    },
    hide : function( ) {}
  };

  $.fn.snake = function( method ) {

//    console.log( call:   + method);

    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method ===  object  || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error(  Method   +  method +   does not exist on jQuery.tooltip  );
    }    

  };

})( jQuery );

现在我需要打电话<代码>run。 采用<代码>init方法。 如何做到这一点?

最佳回答

Ah sorry dudes——现在找到答案。 展望:

methods.run.call( this );
问题回答

暂无回答




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签