在不使用 JS 的情况下,防止 HTML 中的图像可拖动或可选
将以下代码段添加到图像属性中,并防止拖动和选择图像。
img {
user-drag: none;
user-select: none;
-moz-user-select: none;
-webkit-user-drag: none;
-webkit-user-select: none;
-ms-user-select: none;
}在文本或图像上双击后,文本或图像将突出显示(选中)。可以使用 user select 属性来防止这种情况。通过将此属性设置为 none,我们可以防止选中(高亮显示)我们的图像。
示例
你可以尝试运行以下代码以防止选中图像 −
<!DOCTYPE html>
<html>
<head>
<style>
img {
-drag: none;
user-select: none;
-moz-user-select: none;
-webkit-user-drag: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>
</head>
<body>
<img src = "https://tutorialspoint.com/images/python3.png" alt = "Python" width = "62" height = "62">
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP