js 获取鼠标的位置(兼容火狐与ie)用了jquery 进行简化操作,但获取鼠标的是纯js,加上<!DOCTYPE 标准
<!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">
td
{
border: solid 1px #185D98;
width: 60px;
text-align: center;
padding: 8px;
}
</style>
<script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
</head>
<body style="text-align: center; margin: 5px auto;">
<form id="form1" runat="server">
<div style="margin: 5px auto; width:100%; border:solid 1px #F7D1A5; text-align:center;">
<table id="mytable" cellpadding="0" cellspacing="0">
</table>
<div style="width: 200px; height: 80px; position: absolute; background-color: #DB0C0C"
id="mydiv">
</div>
<script type="text/javascript">
$(function() {
var table = $("#mytable");
for (var i = 0; i < 200; i++) {
table.append("<tr><td>" + i * 10 + "</td><td>" + i * 10 + "</td><td>" + i * 10 + "</td><td>" + i * 10 + "</td><td>" + i * 10 + "</td><td>" + i * 10 + "</td><td>" + i * 10 + "</td><td>" + i * 10 + "</td><td>" + (i * 10 + 1) + "</td><td>" + (i * 10 + 2) + "</td><td>" + (i * 10 + 1) + "</td><td>" + (i * 10 + 1) + "</td><td>" + (i * 10 + 1) + "</td><td>" + (i * 10 + 1) + "</td><td>" + (i * 10 + 1) + "</td><td>" + (i * 10 + 1) + "</td></tr>");
}
var mydiv = $("#mydiv");
table.find("tr>td").click(function(e) {
var x = document.documentElement.scrollLeft + e.clientX;
var y = document.documentElement.scrollTop + e.clientY;
var width = document.documentElement.clientWidth;
var height = document.documentElement.clientHeight;
//var width = document.documentElement.scrollWidth;
//var height = document.documentElement.scrollHeight;
var divwidth = mydiv.attr("offsetWidth");
var divheight = mydiv.attr("offsetHeight");
if (x + divwidth > width) {
x = x - divwidth;
}
if (y + divheight > height) {
y = y - divheight;
}
mydiv.css("left", (x + "px")).css("top", (y + "px"));
});
});
</script>
</div>
</form>
</body>
</html>
posted on 2010-07-09 00:00 jianshaohui 阅读(5088) 评论(0) 编辑 收藏 举报