2013-06-20 32 views
4

一個奇怪的問題。如何從元素中解除聚焦事件

腳本在'mousedown'上做了一些事情,然後在'keypress','focusout','keyup'事件上執行。

問題是,一旦執行其中一個事件,我該如何殺死兩個剩餘的事件。

我試過$(document).off()$(this).off('focusout'),沒有任何工作。

任何線索?

.on('mousedown', '.task-content', function() { 
    // Make DIV Editable 
}) 

.on('keypress', '.task-content', function (e) { 
    if (e.which == 13 && !e.shiftKey) { 
     // Update DIV content to DB 
    } 
}) 

.on('focusout', '.task-content', function() { 
    // Update DIV content to DB 
}) 

.on('keyup', '.task-content', function (e) { 
    if (e.which == 27) { 
    // Return DIV to previous state 
    } 
}) 
+1

我不明白你想要做什麼。您能否顯示您嘗試取消/解除綁定事件的實際代碼? – Mathletics

+0

@Mathletics /我已經編輯了一些代碼(這些工作很好順便說一句 - 這將是一個長的代碼發佈)。具體發生的是,一旦我點擊「Enter」(按鍵,13)//聚焦點仍然是「聆聽」並且可以執行。請參閱我希望一旦使用其中一個進程就會殺死另外兩個進程。 –

+0

我想他只想執行3個處理程序中的1個,解除綁定並不是必需的。 –

回答

1

你的代碼工作正常,也許你可能會指出錯誤selector

$('.task-content').on('keypress', function (e) { 
    if (e.which == 13 && !e.shiftKey) { 
     // Update DIV content to DB 
     alert("key press"); 
     $(this).off('focusout'); 
    } 
}) 

$('.task-content').on('focusout', function() { 
    // Update DIV content to DB 
    alert("focus out "); 
}) 

檢查此JSFiddle

+0

感謝您的支持。我只是設法做到了這一點,但是我需要再次'重新使用'專注於'mousedown'。 //換句話說,它不會因爲好事而被殺害。 –

+0

@October使用委託將重新綁定它。 – Praveen

+0

是不是由.on()代替的委託? –

相關問題