简体   繁体   中英

Disable dragging for all images

So, I want to disable dragging on all images on a particular page. I've made this work with ondragstart="return false" , but I hate working with HTML attributes when I can make it work with JQuery (especially since it involves a lot of images). The code I've got is:

const img = document.getElementsByTagName("img")
for (let i = 0; i < img.length; i++) {
    img[i].addEventListener("drag", function(a) {
        a.preventDefault;
    });
}

When I look in the developer tools I can see the event listener on every image, but I can still drag them. What's going on here? By the way, I've also tried function() {return false} .

How about...

HTML ondragstart Event Attribute

<body>
    <img src="html5.png" id="html5" width="256px" height="256px" alt="HTML5" />
</body>

<script>
    document.getElementById('html5').ondragstart = function () { return false; };
</script>

How about...

document.getElementsByClassName("class_values");

How to “get” elements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM