2010-11-24 74 views
2

嘿,那裏,我只是在jsFiddle搞亂了,並試圖做一個簡單的拖放。只要你點擊鼠標中心,就不用擔心偏移等問題。在鼠標下,我返回假,我認爲這會阻止「鬼」出現的元素種類。它沒。我將注意力集中在鼠標身上,以防萬一,但它也沒有幫助。JavaScript:停止選擇可拖動的元素

所以基本上我的問題是,我怎樣才能阻止被選中的元素?你可以在這裏找到小提琴:http://jsfiddle.net/Wolfy87/HY2u4/

任何幫助將不勝感激。謝謝。

編輯:我剛剛在safari中測試它,它很好,到目前爲止它似乎只是Firefox。

回答

1

修正了它。事實證明,我必須設置onmousedown和onselectstart函數。我認爲這與在標籤中放置onmousedown='...'相同。無論如何,它現在是固定的。只要去小提琴頁面。

這基本上是固定它的代碼。

$('div.draggable').attribute().onselectstart = function() { 
    return false; 
} 

$('div.draggable').attribute().onmousedown = function() { 
    return false; 
} 

它停止在Firefox和IE中選擇元素作爲文本的默認操作。

3
jQuery.fn.extend({ 
    disableSelection : function() { 
    this.each(function() { 
     this.onselectstart = function() { return false; }; 
     this.unselectable = "on"; 
     jQuery(this).css('-moz-user-select', 'none'); 
    }); 
    } 
}); 

//How to use: 
$('div.draggable').disableSelection(); 

Soulution沒有jQuery的:

function disableSelection(target){ 
    if (typeof target.onselectstart!="undefined") //IE route 
    target.onselectstart=function(){return false} 
    else if (typeof target.style.MozUserSelect!="undefined") //Firefox route 
    target.style.MozUserSelect="none" 
    else //All other route (ie: Opera) 
    target.onmousedown=function(){return false} 
    target.style.cursor = "default" 
} 

發現:
Is there a way to make text unselectable on an HTML page?

+0

很抱歉,但我不使用jQuery。這是我自己的圖書館。但好的概念,堅持在一個插件。 – Olical 2010-11-24 14:46:17