【问题标题】:jQuery plugin function override from different file来自不同文件的 jQuery 插件函数覆盖
【发布时间】:2014-08-07 02:44:36
【问题描述】:

我想覆盖我的 JS 库中的一个函数。我有以下代码结构

插件.js

function Plugin(elements, options) {
    'use strict';
    return this.init(elements, options);
}

if (typeof module === 'object') {
    module.exports = Plugin;
}

(function (window, document) {
    'use strict';       

    Plugin.prototype = {
        defaults: {
            showFlag: false,
            indent: true                         
        },

        insertDiv: function(parentElement){
           //some jquery code;
        },

        insertCustomData: function(parentElement){
           //some jquery code
           this.insertDiv(parentElement);
        },


    };

}(window, document));

test.html

<html>
<body>
  <div id="test"> 
    <p>Test Content</p>
  </div>
</body>
<script src="plugin.js"/>
<script type="text/javascript">    
    var plugin = new Plugin('test', {showFlag: true, indent:false});                    
</script>
</html>

我想覆盖 plugin.js 中的 insertDiv() 函数,并在我的 test.html 中提供我自己的 insertDiv() 实现

所以当我从 insertCustomData() 调用 insertDiv() 时,它应该在我的 test.html 中而不是在 plugin.js 中执行该函数

有什么方法可以实现吗?

提前致谢。

【问题讨论】:

    标签: javascript jquery jquery-plugins


    【解决方案1】:

    好的,直接覆盖

    <html>
    <body>
      <div id="test"> 
        <p>Test Content</p>
      </div>
    </body>
    <script src="plugin.js"/>
    <script type="text/javascript">    
    
        Plugin.prototype.insertDiv = function() {
            // something else ?
        }
    
        var plugin = new Plugin('test', {showFlag: true, indent:false});                    
    </script>
    </html>
    

    FIDDLE

    【讨论】:

      猜你喜欢
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 2013-09-11
      相关资源
      最近更新 更多