如何覆盖jquery中的现有函数

时间:2014-02-17 07:43:37

标签: javascript jquery javascriptmvc

这是所有js的默认值

getEditor: function(){
    $( '#datatableEditor' ).remove();
    var editor = $( '<div id="datatableEditor" class="popupEditor"/>' );
    $( 'body' ).prepend( editor );

    var dialog = $(editor).dialog({
        title: 'Edit item',
        modal: true,
        width: 'auto',
        height: 'auto'
    });

现在我正在写另一个js。我需要覆盖我的js中的getEditor ...

3 个答案:

答案 0 :(得分:6)

您没有明确地描述您的问题,但是基于您在标题中提到的内容:

  

覆盖jquery中的现有函数

您似乎想要更改jQuery函数,如果是这样的话,它们通常被定义为$.fn.getEditor,您应该这样做:

(function ($) {
    var oldGetEditor = $.fn.getEditor;
    $.fn.getEditor = function() {

       //you can call the oldGetEditor here if you want
    };
})(jQuery);

答案 1 :(得分:0)

您只需获得对“getEditor”函数的引用,并为其指定另一个函数。

getEditor:function(){
// new function will override the existing reference that getEditor already has.
}

答案 2 :(得分:0)

someObject = {
 getEditor: function(){
  $( '#datatableEditor' ).remove();
  var editor = $( '<div id="datatableEditor" class="popupEditor"/>' );
  $( 'body' ).prepend( editor );

  var dialog = $(editor).dialog({
    title: 'Edit item',
    modal: true,
    width: 'auto',
    height: 'auto'
  });
}

所以写一下

someObject.getEditor = function(){
  // your own logic here
}

您必须获取存储getEditor函数的对象,并使用引用来更改它。

相关问题