捕捉鼠标右键单击一个IFRAME使用jquery内部事件 [英] Capture mouse right click event inside a iframe using jquery

查看:354
本文介绍了捕捉鼠标右键单击一个IFRAME使用jquery内部事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用任何JavaScript或jQuery的codeS捕捉鼠标右键单击事件iframe中在asp.net中。

I need to capture right mouse click event inside a iframe in asp.net using any javascript or jquery codes.

下面是code到目前为止,我都试过了。

below is the code i have tried so far.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Detect IFrame Clicks</title>

<script type="text/javascript" language="javascript" src="JScript/jquery-1.7.1.js"></script>

<script type="text/javascript" language="javascript" src="JScript/jquery.ui.core.js"></script>

<script type="text/javascript" language="javascript" src="JScript/jquery.ui.widget.js"></script>

<script type="text/javascript" language="javascript" src="JScript/jquery.ui.mouse.js"></script>

<script type="text/javascript">        
    document.onmousedown = onMousebtClick
    var isOverIFrame = false;
    $(document).ready(function() {
        function processMouseOut() {
            isOverIFrame = false;
            top.focus();
        }

        function processMouseOver() {
            isOverIFrame = true;
        }

        function processIFrameClick() {
            if (isOverIFrame) {
                log("CLICK << detected. ");
                onMousebtClick();
            }
        }

        function log(message) {
            var console = document.getElementById("console");
            var text = console.value;
            text = text + message + "\n";
            console.value = text;
        }

        function attachOnloadEvent(func, obj) {
            if (typeof window.addEventListener != 'undefined') {
                window.addEventListener('load', func, false);
            } else if (typeof document.addEventListener != 'undefined') {
                document.addEventListener('load', func, false);
            } else if (typeof window.attachEvent != 'undefined') {
                window.attachEvent('onload', func);
            } else {
                if (typeof window.onload == 'function') {
                    var oldonload = onload;
                    window.onload = function() {
                        oldonload();
                        func();
                    };
                } else {
                    window.onload = func;
                }
            }
        }

        function init() {
            var element = document.getElementsByTagName("iframe");
            for (var i = 0; i < element.length; i++) {
                element[i].onmouseover = processMouseOver;
                element[i].onmouseout = processMouseOut;
            }
            if (typeof window.attachEvent != 'undefined') {
                top.attachEvent('onblur', processIFrameClick);
            }
            else if (typeof window.addEventListener != 'undefined') {
                top.addEventListener('blur', processIFrameClick, false);
            }
        }

        attachOnloadEvent(init);

    });

    function onMousebtClick() {
        switch (event.button) {
            case 1:
                alert("leftclick");
                break;

            case 2:
                alert("right click");
                break;
        }
    }
    /*document.onmousedown = onMousebtClick*/
</script>

</head>
<body id="mybody">
<iframe src="http://www.microsoft.com" width="800px" height="300px" id="ifrm">
</iframe>
<br />
<br />
<form name="form" id="form" action="">
<textarea name="console" id="console" style="width: 300px; height: 300px;" cols=""
    rows=""></textarea>
<button name="clear" id="clear" type="reset">
    Clear</button>
</form>
</body>
</html>

我可以能够检测到鼠标右键单击在身体标记,但我不能能够检测到iframe内单击鼠标右键的事件。

i can able to detect right mouse click in body tag but i can't able to detect the right mouse click event inside the iframe.

任何人都可以请帮助我。

Can anyone please help me.

推荐答案

有可能的,如果里面的页面中的 IFRAME 是同一个域。

It is possible if the page inside an iframe is on the same domain.

在同一个域中的 IFrame的页面:
http://fiddle.jshell.net/rathoreahsan/MNtw8/8/show/

演示见:
http://jsfiddle.net/rathoreahsan/gJtkW/

而如果你访问内部的其他域的页面中的 IFRAME 就像你正在做的话不推荐。

While if you access the page of an other domain inside an iframe like you are doing then it is not recommended.

编辑:请参见下面给出一些参考,希望这将是有益的。

Edited: See some references given below, hope it will be helpful.


  1. 最iframe的跨域,制定政策问题

跨域的iframe调整大小

这篇关于捕捉鼠标右键单击一个IFRAME使用jquery内部事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆