[JavaScript] jQuery Methoden ansprechen

Dieses Thema im Forum "Webentwicklung" wurde erstellt von onip, 2. November 2010 .

Status des Themas:
Es sind keine weiteren Antworten möglich.
  1. 2. November 2010
    jQuery Methoden ansprechen

    hallo zusammen,

    versuche mir gerade etwas jQuery anzueigene (nem eigentlich lieber MooTools).
    hab mir ein kleines plugin geschrieben und dazu mal eine frage.

    HTML:
    <script type="text/javascript">
    (function($) {
     $.testClass = function ( box, options ){ 
     function trace (txt) {
     alert(txt);
     }
     trace(options.txt);
     };
     
     $.fn.testClass = function ( options ) {
     standardOpt = {
     txt: 'leer'
     }
     options = options || standardOpt;
     this.each( function() {
     new $.testClass( this, options );
     });
     return this;
     };
    
    })(jQuery);
    
    opt = {
     'txt': 'xxx'
    };
    $('body').testClass(opt);
    </script>
    
    meine frage dazu.
    wie kann ich trace() außerhalb des plugin ansprechen?
    ist ein plugin eigentlich das selbe wie eine klasse?

    in MooTools wäre das so
    HTML:
    <script type="text/javascript">
    var testClass = new Class({
     
     Implements: Options,
     options: {
     txt : 'leer'
     },
     initialize: function(options){
     this.setOptions(options);
     this.trace(this.options.txt);
     },
     trace: function(txt){
     alert(txt);
     }
    });
    
    opt = {
     'txt': 'xxx'
    };
    test = new testClass(opt);
    test.trace('yyy');
    </script>
    
     
  2. 2. November 2010
    AW: jQuery Methoden ansprechen

    kommt drauf an ob du die funktion "trace" auch im statischen kontext benötgst.

    ich denk das sollte dir reichen:
    Code:
    (function($) {
     $.testClass = function ( box, options ){ 
     this.trace = function(txt) {
     alert(txt);
     };
     
     this.trace(options.txt);
     };
     
     $.fn.testClass = function ( options ) {
     standardOpt = {
     txt: 'leer'
     }
     options = options || standardOpt;
     this.each( function() {
     new $.testClass( this, options );
     });
     
     return this;
     };
    
    })(jQuery);
     
  3. 2. November 2010
    AW: jQuery Methoden ansprechen

    in der regel eigentlich nicht.
    hier und da kommt es vor, dass ich klassen(plugin) kombiniere und
    werte von einen objekt abfragen möchte.

    HTML:
    opt = {
     'txt': 'xxx'
    };
    test = new testClass(opt);
    alert(test.options.txt);
    
     
  4. 2. November 2010
    AW: jQuery Methoden ansprechen

    mit statisch meinte ich eigl. den nicht-object context (da wo "this" auf die funktion und nicht auf die klasse zeigt )

    Code:
    function EineKlasse()
    {
     function private_statische_funktion()
     {
     // this = private_statische_funktion
     // mach was
     }
    
     this.oeffentliche_methode = function()
     {
     // this = EineKlasse
     private_statische_funktion();
     };
    }
    
    kl = new EineKlasse();
    kl.oeffentliche_methode();
     
  5. 2. November 2010
    AW: jQuery Methoden ansprechen

    danke
     
  6. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.