2016-01-30 26 views
1

實際上,我試圖避免在雙/三點擊文本選擇當用戶使用下拉元素(無錨href屬性)進行交互。這是我創建的插件:jQuery的啓用/禁用文本選擇錯誤

.unselectable 
{ 
    -khtml-user-select: none; 
    -moz-user-select: -moz-none; 
    -ms-user-select: none; 
    -o-user-select: none; 
    -webkit-user-select: none; 
    user-select: none; 
} 

;(function($) 
{ 
    $.fn.disableSelection = function() 
    { 
     return this.each(function() 
     { 
      var element = $(this); 
      element.addClass('unselectable'); 
      element.attr('unselectable', 'on'); 
      element.on('selectstart', false); 
     }); 
    }; 

    $.fn.enableSelection = function() 
    { 
     return this.each(function() 
     { 
      var element = $(this); 
      element.removeClass('unselectable'); 
      element.removeAttr('unselectable', 'on'); 
      element.on('selectstart', true); 
     }); 
    }; 

})(jQuery); 

$('#menu ul li a').click(function(e) 
{ 
    var element = $(this); 
    var target = $('#submenu_' + element.attr('class')); 

    element.disableSelection(); 

    if (target.hasClass('hidden')) 
    { 
     target.hide(); 
     target.removeClass('hidden'); 
    } 

    target.fadeToggle(300, 'swing'); 
    element.enableSelection(); 
}); 

我知道這些函數在jQuery 1.9 blablabla中被棄用,它並不介意。其實,一切都像一種魅力,真的。但我不斷獲取當以下行被處理的錯誤:

element.on('selectstart', true); 

和控制檯說:

Uncaught TypeError: ((n.event.special[g.origType] || (intermediate value)).handle || g.handler).apply is not a function 

如果我取消註釋行成enableSelection功能,錯誤就不再出現,一切都像魅力一樣,但我真的應該努力將這個事件恢復到原來的狀態,否則這個元素在其他情況下就會行爲不端。

你知道這有什麼問題嗎?

回答

1

on函數期望的功能或false作爲處理函數參數。儘管允許使用false作爲function() { return false; }的簡寫,但true的情況並非如此。

要重新啓用的選擇,與off刪除事件處理程序。