简体   繁体   中英

Jquery plugin Retrieve id in function prototype

I got the following jquery plugin code and i want to retrieve the id of the element affected by the plugin. Please help any idea.

I make a fiddle http://jsfiddle.net/bedjoc01/FU8yR/10/

/ ************ Début plugin*****************************************************/

    var sgii_chrono_obj

    (function($){

// chronometre  avec ou sans dixième de seconde
function Osgii_chrono(el, options) {

    // defaults options:
    this.defaults = {

        // afficher au dixième de seconde
        affDixieme: false, 

        // Déclenche le timer automatiquement
        autoStart: false, 

        // afficher le timer dans le div
        affTimer: true
    };

    // extending options:
    this.opts = $.extend({}, this.defaults, options);

    // the element:
    this.$el = $(el);
}

// separate functionality from object creation
Osgii_chrono.prototype = {

    // initializes plugin
    init: function() {
        var _this = this;

Je voudrais retrouver le id qui devrait retourner myChrono

            var monId = ?????
            /* init plugin here */

        this.setOptions(this.opts);
    },

    // sets the new options
    setOptions: function(options){
        var _this = this;
        options = $.extend({}, this.opts, options);

        /* handle changes here */

        //store options
        this.opts = options; 
    },

    // Fonction pour démarrage du chrono
    startChrono: function() {
        var _this = this;
                    var _el = this.$el;
                   //this.html('on est parti');
    },

    // Fonction pour arrêter le chrono
    stopChorno: function() {
        var _this = this;
    },

    // Fonction pour réinitialiser le chrono
    resetChrono: function() {
        var _this = this;
    }
};

// the actual plugin
$.fn.osgii_chrono = function(options) {
    var rev = null, l = this.length;

    if(l && this.each) {
        this.each(function() {
            rev = $(this).data('osgii_chrono');
            if(typeof rev === 'undefined') {
                rev = new Osgii_chrono(this, options);
                rev.init();
                $(this).data('osgii_chrono', rev);
            }
            else if(options) {
                rev.setOptions(options);
            }
        });
    }

    if(l === 1) return rev;
};

    })(jQuery);

html

    <div id="myChronoJb">Avant le start</div>
    <input type="button" value="Start" onclick="objMyChrono.startChrono();">

Javascript calling the plugin

    var objMyChrono
    $(document).ready(function(){

/* add code here */
objMyChrono = $('#myChronoJb').osgii_chrono({ /* options */ });
objMyChrono.startChrono();

Any attribute in jQuery can be retrieved through the attr function.

$("<selector>").attr("<attribute-name>");

id is no exception. In your case it would be:

el.attr("id");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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