我需要在我的页面上放一张图片。我要禁用图像的拖拽。我尝试了很多方法,但都无济于事。有人能帮我一下吗?
我不想保留那个图像作为背景图像因为我在调整图像的大小。
我需要在我的页面上放一张图片。我要禁用图像的拖拽。我尝试了很多方法,但都无济于事。有人能帮我一下吗?
我不想保留那个图像作为背景图像因为我在调整图像的大小。
当前回答
从我自己的答案中,只需在图像上添加一个相同大小的完全透明元素。当你调整图片的大小时,一定要调整元素的大小。
这应该适用于大多数浏览器,甚至更老的浏览器。
其他回答
这段代码正是您想要的。它可以阻止图像拖动,同时允许依赖于该事件的任何其他操作。
$("img").mousedown(function(e){
e.preventDefault()
});
我自己也试过,发现这很有效。
$("img").mousedown(function(){
return false;
});
我确信这将禁用所有图像的拖动。不确定它会影响其他东西。
为图像设置以下CSS属性:
user-drag: none;
user-select: none;
-moz-user-select: none;
-webkit-user-drag: none;
-webkit-user-select: none;
-ms-user-select: none;
document.getElementById('#yourImageId').addEventListener('dragstart', function(e) {
e.preventDefault();
});
工作在这个Plunker http://plnkr.co/edit/HbAbJkF0PVLIMjTmEZml
你可以这样…
document.getElementById('my-image').ondragstart = function() { return false; };
看到它工作(或者不工作)
看起来你正在使用jQuery。
$('img').on('dragstart', function(event) { event.preventDefault(); });