用javascript获取控件实际位置,点击控件紧邻显示浮动 DIV
2012-03-06 14:10
543 查看
代码取自JS日历,提取出来方便使用。欢迎回贴提供改进、更通用的代码:
(注:粘贴即用,兼容 IE7,IE8,IE9的兼容模式,但不兼容IE9。如果想兼容,需在<head>添加<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.dv
{
width:200px;
height:100px;
background-color:Yellow;
border:1px black solid;
position:absolute;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%;">
<tr>
<td>
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="return show(this);"
/>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return show(this);" />
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
<div id="ddv" class="dv">
</div>
<script>
function show(obj) {
var ddv = document.getElementById("ddv");
var fixedX = -1; // x position (-1 if to appear below control)
var fixedY = -1; // x position (-1 if to appear below control)
var leftpos = 0;
var toppos = 0;
var aTag = obj;
do {
aTag = aTag.offsetParent;
leftpos += aTag.offsetLeft;
toppos += aTag.offsetTop;
} while (aTag.tagName != "BODY");
ddv.style.left = fixedX == -1 ? obj.offsetLeft + leftpos : fixedX;
ddv.style.top = (fixedY == -1 ? obj.offsetTop + toppos : fixedY)+20;
return false;
}
</script>
</div>
</form>
</body>
</html>
(注:粘贴即用,兼容 IE7,IE8,IE9的兼容模式,但不兼容IE9。如果想兼容,需在<head>添加<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.dv
{
width:200px;
height:100px;
background-color:Yellow;
border:1px black solid;
position:absolute;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%;">
<tr>
<td>
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="return show(this);"
/>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return show(this);" />
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
<div id="ddv" class="dv">
</div>
<script>
function show(obj) {
var ddv = document.getElementById("ddv");
var fixedX = -1; // x position (-1 if to appear below control)
var fixedY = -1; // x position (-1 if to appear below control)
var leftpos = 0;
var toppos = 0;
var aTag = obj;
do {
aTag = aTag.offsetParent;
leftpos += aTag.offsetLeft;
toppos += aTag.offsetTop;
} while (aTag.tagName != "BODY");
ddv.style.left = fixedX == -1 ? obj.offsetLeft + leftpos : fixedX;
ddv.style.top = (fixedY == -1 ? obj.offsetTop + toppos : fixedY)+20;
return false;
}
</script>
</div>
</form>
</body>
</html>
相关文章推荐
- 获取控件绝对位置,显示div
- javascript与jQuery设置取得div绝对位置一个小应用(像日历控件一样,在编辑框下面显示一个层)
- javascript与jQuery设置取得div绝对位置一个小应用(像日历控件一样,在编辑框下面显示一个层)
- 基于JavaScript实现 获取鼠标点击位置坐标的方法
- 用javascript来显示HTML控件地位置即javascript文件在ASP.NET中地运用
- JAVASCRIPT 提示信息 主要是使用了获取控件的位置进行定位
- MFC 获取picture控件的鼠标点击坐标位置的方法(PreTranslateMessag)
- MFC对话框应用程序 如何在对话框窗口显示前获取窗口(控件)的屏幕坐标位置
- JavaScript中几种获取鼠标点击坐标位置的方法
- JS和JQuery中如何获取和修改控件显示内容(如lable,div,h1)
- JavaScript 获取鼠标点击位置坐标
- HTML 获取当前标签在页面的位置,并且把其他的浮动窗口显示过来
- Win8 获得点击控件的位置,显示上下文菜单
- 基于JavaScript实现 获取鼠标点击位置坐标的方法
- asp.net C#用JavaScript获取JS显示得到TextBox文本框控件text值内容
- js控制div样式显示与隐藏,JS通过点击超链接右边(指定位置)显示一个图标
- 用JavaScript=显示DIV=定时=在一定范围内浮动层
- 在WebBrowser控件中获取鼠标在网页(不是浏览器窗口)上点击的位置,
- JavaScript获取GridView中用户点击控件的行号,列号
- U6数据导出工具项目总结三 鼠标单击或者双击DataGridView控件时获取鼠标点击位置的值