Create JQuery plugin

0

I'm creating a jquery plugin and would like to know how to use the selected div in the plugin to be my variable ... I tried this:

(function ($) {
    $.fn.plugin = function () {
        $(document).ready(function () {
            var video = this;
            video.contextmenu(function () {
                return false;
            });
        });
      };
    })(jQuery);

But it did not work, can anyone tell me how to do it?

link

    
asked by anonymous 15.04.2014 / 22:00

2 answers

2

In your previous code there was this error, plus the wrong use of this within the closure, as indicated in the EDIT below.

Use of this within the plugin method

In this case, this is already the jQuery object itself.

That is, instead of doing this $(this) , use only this .

this in this case is the object used to call the method:

$("#xpto").dumb( ... );

In the plugin, this refers to $("#xpto") itself.

Reference:

link

EDIT:

Problem with this in the closure

In javascript, the this object is linked directly to the current executing method. So you'll have to save the jquery object, in a variable, before using it inside a closure:

(function ($) {
    $.fn.dumb = function () {
        var _this = this;
        $(document).ready(function () {
            var video = _this;
            video.contextmenu(function () {
                return false;
            });
        });
      };
    })(jQuery);
    
15.04.2014 / 22:05
0

You've tried:

(function( $ ){ 
    $.fn.dumb = function() {
        $(document).ready(function () {
            var video = this;
        });
    }
)( jQuery );

I think it might be the difference between $ (this) and this, but I have not tested the code ...

    
15.04.2014 / 22:07