简体   繁体   English

禁止拖动所有图像

[英]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). 我已经使用ondragstart="return false"完成这项工作,但是当我可以使其与JQuery一起使用时(特别是因为它涉及很多图像),我讨厌使用HTML属性。 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} . 顺便说一句,我也尝试过function() {return false}

How about... 怎么样...

HTML ondragstart Event Attribute HTML ondragstart事件属性

<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 如何“获取”元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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