如何在不使用 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>

更新于:01-6 月-2020

10K+ 浏览

开启你的 职业

完成课程获得认证

开始
广告
© . All rights reserved.