Home > Article > Web Front-end > How to Disable Image Dragging in HTML?
Disallowing Image Dragging in HTML
In instances where you want to prevent users from dragging and dropping an image, you can utilize JavaScript to disable this functionality. Here's how you can achieve this:
Method 1:
ondragstart Event:
You can disable image dragging by handling the ondragstart event. By setting the handler function to false, you effectively prevent the browser from initiating the drag operation. Here's an example:
document.getElementById('my-image').ondragstart = function() { return false; };
Method 2:
jQuery:
If you're using jQuery, the following code will disable image dragging for all images on the page:
$('img').on('dragstart', function(event) { event.preventDefault(); });
Note:
By implementing these methods, you can effectively prevent unwanted image dragging, enhancing the usability and security of your web application.
The above is the detailed content of How to Disable Image Dragging in HTML?. For more information, please follow other related articles on the PHP Chinese website!