javascript 使用 jquery 在 iframe 内捕获鼠标右键单击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11223210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capture mouse right click event inside a iframe using jquery
提问by Arun
I need to capture right mouse click event inside a iframe in asp.net using any javascript or jquery codes.
我需要使用任何 javascript 或 jquery 代码在 asp.net 中的 iframe 内捕获鼠标右键单击事件。
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>
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.
我可以在 body 标签中检测到鼠标右键单击,但我无法在 iframe 中检测到鼠标右键单击事件。
Can anyone please help me.
谁能帮帮我吗。
回答by Ahsan Rathod
It is possible if the page inside an iframe
is on the same domain.
如果 an 中的页面在iframe
同一个域中,则是可能的。
IFrame page at same domain:http://fiddle.jshell.net/rathoreahsan/MNtw8/8/show/
同一域的 IFrame 页面:http : //fiddle.jshell.net/rathoreahsan/MNtw8/8/show/
See Demo:http://jsfiddle.net/rathoreahsan/gJtkW/
见演示:http : //jsfiddle.net/rathoreahsan/gJtkW/
While if you access the page of an other domain inside an iframe
like you are doing then it is not recommended.
如果您iframe
像正在做的那样访问其他域的页面,则不建议这样做。
Edited: See some references given below, hope it will be helpful.
编辑:请参阅下面给出的一些参考资料,希望它会有所帮助。
回答by Tats_innit
Hey: this might help:
嘿:这可能有帮助:
How Enable Right Click in IFRAME with JavaScript
如何使用 JavaScript 在 IFRAME 中启用右键单击
or
或者
This is not possible I'm afraid. When you're inside the iframe, you're in a second tag... Unless you own what's inside the iframe and are able to put your Javascript on the page within then it's impossble. iframes are very limited for many many security reasons.
恐怕这是不可能的。当您在 iframe 内时,您就在第二个标签中...除非您拥有 iframe 内的内容并且能够将您的 Javascript 放在页面内,否则这是不可能的。由于许多安全原因,iframe 非常有限。
Please let me know if anyone thinks otherwise, happy to take this post off.
如果有人有不同的看法,请告诉我,很高兴撤下这篇文章。
AlthoughTry this hack mentioned here: http://www.codingforums.com/archive/index.php/t-8824.html
虽然试试这里提到的这个黑客:http: //www.codingforums.com/archive/index.php/t-8824.html
orhere http://forums.mozillazine.org/viewtopic.php?f=19&t=371334
或在这里http://forums.mozillazine.org/viewtopic.php?f=19&t=371334
cheers,
干杯,