简体   繁体   English

模拟右键单击图像

[英]Simulate Right-Click on Image

When an image on a web-page is right-clicked a menu appears which provides the ability to "Save Image As". 右键单击网页上的图像时,将显示一个菜单,该菜单提供了“将图像另存为”的功能。 Is it possible to get this functionality through jQuery or JavaScript by clicking on a button (div). 是否可以通过单击按钮(div)通过jQuery或JavaScript获得此功能。

I've tried to get code from numerable sites without success. 我试图从无数站点获取代码而没有成功。

The following is a simple test page where clicking on the red button would show the menu. 以下是一个简单的测试页面,单击红色按钮将显示菜单。

Thanks, 谢谢,

<!DOCTYPE HTML>
<html>
  <head>
    <title>Test</title>
    <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1.0"/>
    <script src='jquery-2.1.4.js'></script>
    <style>
        #divBtn{
            width: 100px;
            height: 50px;
            top: 0px;
            left: 0px;
            background-color: #ff0000;
            position: absolute;
        }
        #divImage {
            width: 864px;
            height: 541px;
            top: 50px;
            left: 0px;
            position: absolute;
        }
    </style>
  </head>
  <body>
    <a href="Imager.jpg" download="ImageCopy.jpg"><div id="divBtn"></div>Click</div></a>
    <div id="divImage">
        <img src='Imager.jpg'></img>
    </div>

    <script>
        $(document).ready(function(){

            //Code to show menu when divBtn is clicked.

        });
    </script>

  </body>
</html>

This is not possible to do. 这是不可能的。 You can however create your own "menu" through the use of and trigger it with the right click event. 但是,您可以通过使用创建自己的“菜单”,并通过右键单击事件触发它。 You cannot access the browser's right click menu. 您无法访问浏览器的右键菜单。

You can trigger download of an image using HTML5 download attribute. 您可以使用HTML5下载属性触发图像的下载。 More info here: Simulate Save Image As 此处更多信息: 模拟另存为

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

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