JavaScript:停止选择可拖动元素

发布于 2024-10-04 13:18:47 字数 373 浏览 4 评论 0原文

嘿,我只是在 jsFiddle 中乱搞,并尝试进行简单的拖放操作。我没有费心去获取偏移量等,所以一旦你点击它,它就会以你的鼠标为中心。当鼠标按下时,我返回 false,我认为这会阻止“幽灵”元素的出现。但事实并非如此。我将鼠标放在身体上,以防万一,但这也没有帮助。

所以基本上我的问题是,如何停止选择元素?您可以在这里找到小提琴: http://jsfiddle.net/Wolfy87/HY2u4/

任何帮助都会非常感谢。谢谢。

编辑:我刚刚在 safari 中测试了它,它很好,到目前为止它似乎只有 firefox。

Hey there, I was just messing about in jsFiddle and attempted to make a simple drag and drop. I did not bother with getting the offset etc so as soon as you click it centers around your mouse. On mouse down I return false which I thought would stop the elements kind of 'ghost' from appearing. It did not. I focus onto the body on mouse down just in case it was that but it did not help either.

So basically my question is, how can I stop elements being selected? You can find the fiddle here: http://jsfiddle.net/Wolfy87/HY2u4/

Any help would be much appreciated. Thanks.

EDIT: I just tested it in safari and it is fine, so far it only seems to be firefox.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花细雨 2024-10-11 13:18:47
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" 
} 

找到于:
有没有办法使 HTML 页面上的文本变得不可选择?

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 without 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" 
} 

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

无人接听 2024-10-11 13:18:47

修好了。原来我要设置onmousedown和onselectstart函数。我认为这与在标记中放置 onmousedown='...' 相同。无论如何,现在已经修复了。只需转到小提琴页面即可。

这基本上是修复它的代码。

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

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

它会停止在 Firefox 和 IE 中选择元素作为文本的默认操作。

Fixed it. It turns out that I have to set the onmousedown and onselectstart functions. I assume this is the same as placing onmousedown='...' in the tag. Anyway, it is fixed now. Just go to the fiddle page.

This is basically the code that fixed it.

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

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

It stops the default action of selecting the element as text in Firefox and IE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文