Add function to existing JQuery plugin

前端 未结 2 1005
再見小時候
再見小時候 2021-01-13 22:57

Is it possible to add a function to a plugin without modifying the actual plugin? Can I do something like this in my site\'s js file?

$.fn.Watermark.Refresh          


        
相关标签:
2条回答
  • 2021-01-13 23:20

    You can add those functions if you want to, but you'll have to make sure that you're also loading jQuery itself and the plugin to be modified. If you're getting those errors (that jQuery or "$" are not defined), then you have not correctly done that.

    Now, though it's true that you can add those functions, I have to wonder what the point would be. If I were to do this, for example:

    $.fn.css.myFunction = function() { return "hello world"; };
    

    then it would be possible to call it:

    var str = $.fn.css.myFunction();
    

    but so what? What good does that do me? I don't think it's very useful.

    0 讨论(0)
  • 2021-01-13 23:29

    Make sure you are including the plugin after jQuery.

    0 讨论(0)
提交回复
热议问题