2013-07-03 62 views
2

我有一個IFRAME內的內容,當鼠標單擊時,我需要找到頂部和左側的座標。jquery在IFRAME中找到鼠標單擊位置

我使用下面的代碼來查找頂部和左側的座標,但頂部(e.pageY)座標給出錯誤的位置,而IFRAME滾動。

var iframeDoc = $('#bookcontentHeight').contents().get(0); 
// Bind event to iframe document 
$(iframeDoc).bind('mouseup', function(e) {      
    e.preventDefault();     
    $("#custom-menu").css({ 
     top: e.pageY + "px", left: e.pageX + "px" 
    }).show(100); 
}); 

回答

0

注: - 代碼是JS

<head> 
<script language="JavaScript"> 
function point_it(event){ 
    pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("pointer_div").offsetLeft; 
    pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("pointer_div").offsetTop; 
    document.getElementById("cross").style.left = (pos_x-1) ; 
    document.getElementById("cross").style.top = (pos_y-15) ; 
    document.getElementById("cross").style.visibility = "visible" ; 
    document.pointform.form_x.value = pos_x; 
    document.pointform.form_y.value = pos_y; 
} 
</script> 
</head> 
<body> 
<form name="pointform" method="post"> 
<div id="pointer_div" onclick="point_it(event)" style = "background-image:url('sun.jpg');width:500px;height:333px;"> 
<img src="point.gif" id="cross" style="position:relative;visibility:hidden;z-index:2;"></div> 
You pointed on x = <input type="text" name="form_x" size="4" /> - y = <input type="text" name="form_y" size="4" /> 
</form> 
</body> 
</html> 

這是工作得到它在jQuery的等價物。