不使用 JS 阻止 HTML 中的图片被拖动或选中
htmlweb developmentfront end technology更新于 2026/1/17 17:48:17
将以下代码片段添加到图片属性中,即可阻止图片被拖动和选中。
img {
user-drag: none;
user-select: none;
-moz-user-select: none;
-webkit-user-drag: none;
-webkit-user-select: none;
-ms-user-select: none;
}
双击文本或图片时,它会被高亮显示(选中)。可以使用用户选择属性来阻止这种情况。通过将此属性设置为 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://www.cainiaomax.com/images/python3.png" alt = "Python" width = "62" height = "62">
</body>
</html>
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

