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功能,錯誤就不再出現,一切都像魅力一樣,但我真的應該努力將這個事件恢復到原來的狀態,否則這個元素在其他情況下就會行爲不端。
你知道這有什麼問題嗎?