var tableParent = document.getElementById("TargetTable");
var parentWidth = tableParent.offsetWidth;
var parentHeight = tableParent.offsetHeight;
这两句没取到值,这样改下就可以了:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<body>
<table style="width: 452px; height: 379px; " border=1 id="TargetTable" ><tr>
<td style="width: 7px; height: 2px" id="TDFirst">
</td>
<td >
</td>
</tr>
<tr>
<td id="TDSecond">
</td>
<td height="100%" style="overflow:hidden;" align="left" valign="top" onmousedown="OnMouseDown1(this)" onmousemove="OnMouseMove1(this)" onmouseup="OnMouseUp1(this)" >
12</td>
</tr></table></body><script type="text/javascript">
var currentClickObj = null;
var currentMoveObj = document.getElementById("TDFirst"); //当前拖动对象
var relWidth; //鼠标按下位置相对对象位置
var relHeight;
var elX;
var elY;
var mouseDownY;
var mouseDownX;var zindex=-1;//控制被拖动对象的z-index值(对象在页面中的位置)
var tableParent = document.getElementById("TargetTable");
var parentWidth = tableParent.offsetWidth;
var parentHeight = tableParent.offsetHeight;
//鼠标按下
function OnMouseDown1(obj)
{
currentClickObj = obj; //当对象被按下时,记录该对象currentMoveObj = document.getElementById("TDFirst");
tableParent = document.getElementById("TargetTable");
relWidth = currentMoveObj.offsetWidth;
relHeight = currentMoveObj.offsetHeight;
zindex=currentClickObj.style.zIndex; //记录原z-index值
currentClickObj.style.zIndex=2;
currentClickObj.setCapture(); //事件捕获
mouseDownY = event.clientY;
mouseDownX = event.clientX;
// parentWidth = tableParent.offsetWidth;
// parentHeight = tableParent.offsetHeight;}
//
//鼠标移动
//
function OnMouseMove1(obj)
{
if(currentClickObj != null)
{var TDSecond2 = document.getElementById("TDSecond");
currentMoveObj = document.getElementById("TDFirst");
var newWidth =relWidth + event.clientX-mouseDownX ;
if(newWidth<1) newWidht=1;//防左移出 offsetWidth253Longif(newWidth >parentWidth - 1) //防右移出
{
newLeft=parentWidth - 1;}
currentMoveObj.style.pixelWidth = newWidth;
// currentClickObj.style.pixelWidth = parentWidth - newWidth;
// TDSecond2.style.pixelHeight = parentHeight - newHeight;var newHeight =relHeight + event.clientY-mouseDownY;if(newHeight<1) newHeight = 1;//防上移出
if(newHeight > parentHeight - 25 ) //防下移出
{
newHeight=parentHeight - 25;}currentMoveObj.style.pixelHeight = newHeight;
// currentClickObj.style.pixelHeight = parentHeight - newHeight;
// TDSecond2.style.pixelHeight = parentHeight - newHeight;}
}
//
//释放鼠标
//
function OnMouseUp1()
{
if(currentClickObj != null)
{
currentClickObj.style.zIndex=zindex; //恢复zIndex
zindex=-1;
currentClickObj.releaseCapture(); //当鼠标释放时同时释放事件捕获
currentClickObj = null; //当鼠标释放时同时释放拖动对象
}
}
</script>
</html>
var parentWidth = tableParent.offsetWidth;
var parentHeight = tableParent.offsetHeight;
这两句没取到值,这样改下就可以了:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<body>
<table style="width: 452px; height: 379px; " border=1 id="TargetTable" ><tr>
<td style="width: 7px; height: 2px" id="TDFirst">
</td>
<td >
</td>
</tr>
<tr>
<td id="TDSecond">
</td>
<td height="100%" style="overflow:hidden;" align="left" valign="top" onmousedown="OnMouseDown1(this)" onmousemove="OnMouseMove1(this)" onmouseup="OnMouseUp1(this)" >
12</td>
</tr></table></body><script type="text/javascript">
var currentClickObj = null;
var currentMoveObj = document.getElementById("TDFirst"); //当前拖动对象
var relWidth; //鼠标按下位置相对对象位置
var relHeight;
var elX;
var elY;
var mouseDownY;
var mouseDownX;var zindex=-1;//控制被拖动对象的z-index值(对象在页面中的位置)
var tableParent = document.getElementById("TargetTable");
var parentWidth = tableParent.offsetWidth;
var parentHeight = tableParent.offsetHeight;
//鼠标按下
function OnMouseDown1(obj)
{
currentClickObj = obj; //当对象被按下时,记录该对象currentMoveObj = document.getElementById("TDFirst");
tableParent = document.getElementById("TargetTable");
relWidth = currentMoveObj.offsetWidth;
relHeight = currentMoveObj.offsetHeight;
zindex=currentClickObj.style.zIndex; //记录原z-index值
currentClickObj.style.zIndex=2;
currentClickObj.setCapture(); //事件捕获
mouseDownY = event.clientY;
mouseDownX = event.clientX;
// parentWidth = tableParent.offsetWidth;
// parentHeight = tableParent.offsetHeight;}
//
//鼠标移动
//
function OnMouseMove1(obj)
{
if(currentClickObj != null)
{var TDSecond2 = document.getElementById("TDSecond");
currentMoveObj = document.getElementById("TDFirst");
var newWidth =relWidth + event.clientX-mouseDownX ;
if(newWidth<1) newWidht=1;//防左移出 offsetWidth253Longif(newWidth >parentWidth - 1) //防右移出
{
newLeft=parentWidth - 1;}
currentMoveObj.style.pixelWidth = newWidth;
// currentClickObj.style.pixelWidth = parentWidth - newWidth;
// TDSecond2.style.pixelHeight = parentHeight - newHeight;var newHeight =relHeight + event.clientY-mouseDownY;if(newHeight<1) newHeight = 1;//防上移出
if(newHeight > parentHeight - 25 ) //防下移出
{
newHeight=parentHeight - 25;}currentMoveObj.style.pixelHeight = newHeight;
// currentClickObj.style.pixelHeight = parentHeight - newHeight;
// TDSecond2.style.pixelHeight = parentHeight - newHeight;}
}
//
//释放鼠标
//
function OnMouseUp1()
{
if(currentClickObj != null)
{
currentClickObj.style.zIndex=zindex; //恢复zIndex
zindex=-1;
currentClickObj.releaseCapture(); //当鼠标释放时同时释放事件捕获
currentClickObj = null; //当鼠标释放时同时释放拖动对象
}
}
</script>
</html>
解决方案 »
- JavaScript,帮忙看下,怎么运行怎么来,老是出现问题呀!
- 制作的仿京东菜单出问题了
- js获取收藏夹内容
- 请教confirm删除数据库信息的用法,谢谢!
- 请教:网页内REALONE插件禁右键以及禁止浮动小按钮
- 请问一下这样的一个正则要怎么写呀!
- 如何在源代码中显示script中的值
- javascript怎么控制div内的的滚动条位置
- 如何实现类似网络蚂蚁右键功能!!!
- ===== 请帮忙给出这断程序的注释,让不太懂js的人能看懂它 =====
- 请问Microsoft VM 对javascript的是否有影响
- 如何在html中取textarea的内容并传到另一个html页的table中!只有10分了!
麻烦你在 http://community.csdn.net/Expert/topic/5591/5591136.xml?temp=.611828 这个帖子里回个话
分一并给你
<head>
<title>Untitled Page</title>
</head> <script type="text/javascript">
var currentClickObj = null;
var currentMoveObj = document.getElementById("TDFirst"); //当前拖动对象
var relWidth; //鼠标按下位置相对对象位置
var relHeight;
var elX;
var elY;
var mouseDownY;
var mouseDownX;
var zindex=-1;//控制被拖动对象的z-index值(对象在页面中的位置) /*----------------------这会出错的,应该在onload中初始化-------------------
// var tableParent = document.getElementById("TargetTable");
// var parentWidth = tableParent.offsetWidth;
// var parentHeight = tableParent.offsetHeight;
*/ var tableParent=null;
var parentWidth=0;
var parentHeight=0;window.onload=function()
{
tableParent = document.getElementById("TargetTable");
parentWidth = tableParent.offsetWidth;
parentHeight = tableParent.offsetHeight;
}
//鼠标按下
function OnMouseDown1(obj)
{
currentClickObj = obj; //当对象被按下时,记录该对象
currentMoveObj = document.getElementById("TDFirst");
tableParent = document.getElementById("TargetTable");
relWidth = currentMoveObj.offsetWidth;
relHeight = currentMoveObj.offsetHeight;
zindex=currentClickObj.style.zIndex; //记录原z-index值
currentClickObj.style.zIndex=2;
currentClickObj.setCapture(); //事件捕获
mouseDownY = event.clientY;
mouseDownX = event.clientX;
// parentWidth = tableParent.offsetWidth;
// parentHeight = tableParent.offsetHeight;
}
//
//鼠标移动
//
function OnMouseMove1(obj)
{
if(currentClickObj != null)
{
var TDSecond2 = document.getElementById("TDSecond");
currentMoveObj = document.getElementById("TDFirst");
tableParent = document.getElementById("TargetTable");
var newWidth =relWidth + event.clientX-mouseDownX ;
if(newWidth<1) newWidht=1;//防左移出 offsetWidth253Long if(newWidth >parentWidth - 1) //防右移出
{
newLeft=parentWidth - 1;
}
currentMoveObj.style.pixelWidth = newWidth;
// currentClickObj.style.pixelWidth = parentWidth - newWidth;
// TDSecond2.style.pixelHeight = parentHeight - newHeight;
var newHeight =relHeight + event.clientY-mouseDownY;
if(newHeight<1) newHeight = 1;//防上移出
if(newHeight > parentHeight - tableParent.offsetTop-15 ) //防下移出 <--------------------修改这里
{
newHeight=parentHeight - tableParent.offsetTop-15; //<--------------------修改这里
}
currentMoveObj.style.pixelHeight = newHeight;
// currentClickObj.style.pixelHeight = parentHeight - newHeight;
// TDSecond2.style.pixelHeight = parentHeight - newHeight;
}
}
//
//释放鼠标
//
function OnMouseUp1()
{
if(currentClickObj != null)
{
currentClickObj.style.zIndex=zindex; //恢复zIndex
zindex=-1;
currentClickObj.releaseCapture(); //当鼠标释放时同时释放事件捕获
currentClickObj = null; //当鼠标释放时同时释放拖动对象
}
}
</script> <body>
<table style="width: 452px; height: 379px; " border=1 id="TargetTable" >
<tr>
<td style="width: 7px; height: 2px" id="TDFirst">
</td>
<td >
</td>
</tr>
<tr>
<td id="TDSecond">
</td>
<td height="100%" style="overflow:hidden;" align="left" valign="top" onmousedown="OnMouseDown1(this)" onmousemove="OnMouseMove1(this)" onmouseup="OnMouseUp1(this)" >
12</td>
</tr>
</table></body>
</html>