2011-04-30 49 views
5

我正在使用jQuery editinPlace插件,在位編輯的默認方式是在選擇器上使用單擊事件,但我試圖做的方式是通過上下文菜單它調用一個函數「rename();」。那麼如何阻止click事件的內聯編輯。請分享您對如何做到這一點一些想法...在函數上調用Editinplace jquery插件而不是點擊

$('.context').editInPlace({ 
     callback: function(idOfEditor) { 
     var renameUrl = "http://www.google.com/"+tablerowId+"/"+enteredText+""; 
     return enteredText; 
     } 
    }); 

回答

1

打開/jquery.editinplace.js源文件。 (網絡版>http://code.google.com/p/jquery-in-place-editor/source/browse/trunk/lib/jquery.editinplace.js

在第一個函數聲明$.fn.editInPlace線#26,更改以下行:

new InlineEditor(settings, dom).init(); 

到>

dom.theEditor = new InlineEditor(settings, dom); 
dom.theEditor.init(); 
dom.data("theEditor", dom.theEditor); 

現在上下文菜單的單擊事件中函數,調用這個>

$("#myContextMenuElement").live("click", function (e) { 
        //your other code 
        rename(e); //you need to pass the event argument to it now 
}); 

確保將'e'傳遞給它。

,並在重命名功能>

function rename(e) { 
    $("#myElementToEditInPlace").data("theEditor").openEditor(e); 
} 

的作品就像一個魅力!

編輯:

爲了確保您不允許用戶活動編輯器通過點擊對自身>使用此代碼:

var myDelegate = { 
     shouldOpenEditInPlace: function (a, b, c) { 
     if (c.target.id != "idOfYourContextElement") { //if the edit was not invoked through the context menu option 
       return false; //cancel the editor open event 
     } 
     return true; 
    } 
}; 

,並在初始化添加代表>

$('.context').editInPlace({ 
     callback: function(idOfEditor) { 
      var renameUrl = "http://www.google.com/"+tablerowId+"/"+enteredText+""; 
      return enteredText; 
     }, 
     delegate: del 
    }); 
+0

它工作正常第一次...一旦我點擊上下文菜單,editplace工作正常,但下一次,如果我直接點擊clas ('。renameFunc')。live(「click」,function(e))這是一個簡單的例子, { \t $( 「上下文」)editInPlace({ \t \t \t回調:函數(idOfEditor,enteredText,orinalHTMLContent,settingsParams,animationCallbacks){ \t \t \t回報enteredText; \t \t \t} \t}); \t重命名(e); }); (e){(「。context」)。data(「theEditor」)。openEditor(e); } – Sullan 2011-05-05 06:31:33

+0

如何在第一次觸發後分離editinplace事件..我嘗試像這樣解除綁定(unbind('。editInPlace')),但是如果我使用這個,我不會在元素上做editinplace第一次.. – Sullan 2011-05-05 06:36:36

+0

增加了防止直接初始化代碼的代碼。 – neebz 2011-05-05 17:04:36