我需要使用任何 javascript 或 jquery 代码在 asp.net 的 iframe 中捕获鼠标右键单击事件。
下面是我到目前为止尝试过的代码。
<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 内的鼠标右键单击事件。
谁能帮帮我吗。