js mousedown事件怎么获取座标? 现在页面上有一个显示的div ,我想 触发 mousedown事件,如果鼠标不是在div的范围内,那么就把div给隐藏,不知道如何实现,请高手指点!最好兼容 firefox和ie 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以给div添加一个attribute,isin,如果鼠标在div范围内就是true,否则是false,当发生mousedown时检测isin就可以了 获取某个元素的坐标。function findPosition(oElement) { var x2 = 0; var y2 = 0; var width = oElement.offsetWidth; var height = oElement.offsetHeight; // alert(width + "=" + height); if (typeof (oElement.offsetParent) != 'undefined') { for (var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) { posX += oElement.offsetLeft; posY += oElement.offsetTop; } x2 = posX + width; y2 = posY + height; return [posX, posY, x2, y2]; } else { x2 = oElement.x + width; y2 = oElement.y + height; return [oElement.x, oElement.y, x2, y2]; }} [oElement.x, oElement.y, x2, y2] 里面的四个对象 具体是指什么??呵呵 我是新手! //获得某个节点的相对坐标function GetElementPoint(object){ var x=0,y=0; while(object.offsetParent) { x+=object.offsetLeft; y+=object.offsetTop; object=object.offsetParent; } x+=object.offsetLeft; y+=object.offsetTop; return {'x':x,'y':y};} 你的思路有点问题,跟获取鼠标无关<body><div id="odiv" style="width:100px;height:100px;border:1px solid red;background-color:#D2D2D2"></div><script type="text/javascript"> var O = document.getElementById('odiv'); document.onmousedown = function() { O.style.display = "none"; } O.onmousedown = function(e) { !!e ? e.stopPropagation() : event.cancelBubble = true }</script></body> <html><head><title> </title></head><body onmousedown="document.getElementById('div').style.display='none'"><div id="div" style="cursor:hand;width:200px;height:200px; background-color:red;" onmousedown="if(window.event) window.event.cancelBubble=true; else arguments[0].stopPropagation();"></div> </body><html> $(document).mousedown(function(event) { var left = calculateOffset(globalSearchSelector, "offsetLeft"); var top = calculateOffset(globalSearchSelector, "offsetTop"); var width = globalSearchSelector.width(); var height = globalSearchSelector.height(); var top1 = calculateOffset(nav_SearchKeyWord, "offsetTop"); var height1 = nav_SearchKeyWord.height(); //在搜索框和提示层范围外清除提示 if (event.clientX < left || event.clientX > left + width) { clearItems(); } else { if (event.clientY < top || event.clientY > height + top) { if (event.clientY < top1 || event.clientY > height1 + top1) { clearItems(); } } } }); JSON 这么处理咋个会有错啊???????????? 动态获取的图片实现翻滚 请教一个很简单的问题:变量怎么表示? 保存网页标注效果 求解一个正则表达式 关于用execScript函数,弹出对话框标题栏的问题!有兴趣的朋友来看看。 单选的问题! 想各位高手请教一个下拉菜单的问题,急,在线等 大拿,请解惑,关于tinyMCE,百思不得其解 用opener可以吗? 各位大哥大姐 帮帮忙 ~~急求~~~~~~~~~~ 【【【送分了,教我2个简单的代码】】】】
function findPosition(oElement) {
var x2 = 0;
var y2 = 0;
var width = oElement.offsetWidth;
var height = oElement.offsetHeight;
// alert(width + "=" + height);
if (typeof (oElement.offsetParent) != 'undefined') {
for (var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) {
posX += oElement.offsetLeft;
posY += oElement.offsetTop;
}
x2 = posX + width;
y2 = posY + height;
return [posX, posY, x2, y2]; } else {
x2 = oElement.x + width;
y2 = oElement.y + height;
return [oElement.x, oElement.y, x2, y2];
}
}
呵呵 我是新手!
function GetElementPoint(object)
{
var x=0,y=0;
while(object.offsetParent)
{
x+=object.offsetLeft;
y+=object.offsetTop;
object=object.offsetParent;
}
x+=object.offsetLeft;
y+=object.offsetTop;
return {'x':x,'y':y};
}
<div id="odiv" style="width:100px;height:100px;border:1px solid red;background-color:#D2D2D2"></div>
<script type="text/javascript">
var O = document.getElementById('odiv');
document.onmousedown = function() {
O.style.display = "none";
}
O.onmousedown = function(e) {
!!e ? e.stopPropagation() : event.cancelBubble = true
}
</script>
</body>
<head>
<title> </title>
</head>
<body onmousedown="document.getElementById('div').style.display='none'">
<div id="div" style="cursor:hand;width:200px;height:200px; background-color:red;" onmousedown="if(window.event) window.event.cancelBubble=true; else arguments[0].stopPropagation();"></div>
</body>
<html>
var left = calculateOffset(globalSearchSelector, "offsetLeft");
var top = calculateOffset(globalSearchSelector, "offsetTop");
var width = globalSearchSelector.width();
var height = globalSearchSelector.height();
var top1 = calculateOffset(nav_SearchKeyWord, "offsetTop");
var height1 = nav_SearchKeyWord.height();
//在搜索框和提示层范围外清除提示
if (event.clientX < left || event.clientX > left + width) {
clearItems();
}
else {
if (event.clientY < top || event.clientY > height + top) {
if (event.clientY < top1 || event.clientY > height1 + top1) {
clearItems();
}
}
}
});