嘿,我只是在 jsFiddle 中乱搞,并尝试进行简单的拖放操作。我没有费心去获取偏移量等,所以一旦你点击它,它就会以你的鼠标为中心。当鼠标按下时,我返回 false,我认为这会阻止“幽灵”元素的出现。它没。鼠标按下时我将注意力集中在 body 上,以防万一,但这也没有帮助。
所以基本上我的问题是,如何停止选择元素?您可以在这里找到 fiddle :http://jsfiddle.net/Wolfy87/HY2u4/
任何帮助将不胜感激。谢谢。
编辑:我刚刚在 safari 中测试了它,它很好,到目前为止它似乎只有 firefox。
最佳答案
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();
没有 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?
关于JavaScript:停止选择可拖动元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4267374/