javascript 如何禁用鼠标单击任何图像的拖动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7604472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:40:38  来源:igfitidea点击:

How to disable dragging with mouse click any images?

javascriptjquerycsshtml

提问by someone

I have an image <img src="fullscreen.jpg" />for online driving exams, where the questions are asked using image text and with image embedded. When anyone click the image and drag the mouse it moves the image.

我有一张<img src="fullscreen.jpg" />用于在线驾驶考试的图像,其中使用图像文本和嵌入图像来提问。当任何人单击图像并拖动鼠标时,它会移动图像。

But how can i stop this dragging? So that nobody is allowed to do so.

但是我怎样才能阻止这种拖拽呢?这样任何人都不允许这样做。

回答by DzikiMarian

$('#id-of-your-image').mousedown(function(){return false});

Of course you may have to use other selector as in your example image has no id attribute.

当然,您可能必须使用其他选择器,因为在您的示例图像中没有 id 属性。

回答by Jason Barry

If you're using jQuery, you're looking for the dragstartevent.

如果您正在使用 jQuery,那么您正在寻找该dragstart事件。

$("img").bind('dragstart', function(){
    return false; 
});

回答by someone

Try any of these 3 if you can't or don't want to use an idor jQuery(cross-browser performance has not been tested):

如果您不能或不想使用idjQuery(跨浏览器性能尚未测试),请尝试以下 3 个中的任何一个:

<img src="http://www.google.com/intl/en_com/images/srpr/logo3w.png" ondragstart='return false;'/>
<img src="http://www.google.com/intl/en_com/images/srpr/logo3w.png" onmousedown='return false;'/>
<img src="http://www.google.com/intl/en_com/images/srpr/logo3w.png" onmousedown='return false;' ondragstart='return false;'/>

Demo

演示