offsetParent问题再现
时间:2008-06-19 23:54:44
来源:论坛整理 作者: 编辑:chinaitzhe
这个日历框有这样的函数获取位置信息
function getTop(obj) //è?μ?Y
{
var h=0;
h = obj.offsetTop;
if(obj.tagName != "BODY")
h = getTop(obj.offsetParent);
return h;
}
即不断循环,从当前激发日期框的控件知道获取BODY元素。
但是我的程序获取不到BODY,而直接获取了HTML,非常希奇!
调用日期框的页面如下所示:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>index </title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<style>
<!--
.nav01 {color:#000000;font-family:'Arial';}
.nav01 a:link {font-size: 12px;color: #000000;text-decoration: none;}
.nav01 a:visited {font-size: 12px;color:#000000;text-decoration: none;}
.nav01 a:hover {font-size: 12px;color: #000000;text-decoration: underline;}
.content {
margin-left: 0px;
margin-top: 10px;
border: solid #b9c1d0 1px;
background: #e1e5ec;
width:800px;
font-size: 12px;
color: #4e4e4e;
}
.contentTop {
margin-left: 0px;
margin-top: 22px;
border: solid #b9c1d0 1px;
background: #e1e5ec;
width:800px;
height:47px;
font-size: 12px;
color: #4e4e4e;
}
.contentTable {
width: 800px;
height: 100%;
border: 0px;
bordercolor: red;
cellpadding: 0;
cellspacing: 0;
align:left;
}
-->
</style>
</head>
<script type="text/javascript" src="js/calTime.js"> </script>
<script language="JavaScript">
</script>
<body>
<div id="logo" style="margin-left:0px;">
<img src="images/newFace/logo3.gif">
</div>
<form name="queryForm" id="queryForm" method="POST">
<div 。。。
</div>
<input type="hidden" name="type" id="type" value="1" />
<input type="hidden" name="ParamSelectedName" id="ParamSelectedName" />
<input type="hidden" name="ParamSelectedNum" id="ParamSelectedNum" />
<div 。。。 </div>
<div class="content" id="timeBlock">
<table width="800px" height="100%" border="0" bordercolor="red"
cellpadding="0" cellspacing="0" align="left">
<tr>
<td colspan="2" height="40px" valign="top">
<div id="timePointDiv" style="position:absolute; margin-left:93px; margin-top:5px;visibility:visible">
点
<input type="text" name="timePoint" id="TimePoint"
maxlength="19" onFocus="javascript:vDateType='2'"
onKeyUp="DateFormat(this,this.value,event,false,'2')"
onBlur="DateFormat(this,this.value,event,true,'2')"> <img src="img/bdbs/calBtn.gif" onclick="showDateTimeBar(this.previousSibling, '', 'y-m-d 08:00:00', 'test', 'hello world')"> </img>
(yyyy-MM-dd HH:mm:ss) ////主要是这里点击image的时候只能获取到input的一级,见后面说明
</div>
<div </div>
<div </div>
</td>
</tr>
</table>
</div>
<div class="content" id="dataBlock">
<table....... </table>
</div>
<div .... </div>
<div .... <table ..... </table>
</div>
</form>
</body>
</html>
说明:
在注释处的onclick处写alert()测试时,得到如下结果:
this.previousSibling.name = timePoint ///即得到input对象
this.previousSibling.offsetParent.id = timePointDiv ////即得到input上层的DIV对象
this.previousSibling.offsetParent.offsetParent.tagName = HTML ////即直接获得HTML对象,忽略了中间的n层,不知道为什么
还请有经验的大家帮忙看看
网友回复:http://blog.csdn.net/babydavic/archive/2006/09/11/1208312.aspx
网友回复:我看网上都是这样用的
offsetParent=offsetParent.offsetParent;
网友回复:function getTop(obj) //è?μ?Y
{
var h=0;
h = obj.offsetTop;
if(obj.tagName != "BODY")
h = getTop(obj.offsetParent);
return h;
}
这个函数的递归调用。这一句h = getTop(obj.offsetParent); 相当于你说的offsetParent=offsetParent.offsetParent;
我弄不明白的是,怎么着也应该能取到body呀
网友回复:lz 把所有的js都放到 </html> 后面
因为有个加载解析的顺序问题。
网友回复:是说把 <script type="text/javascript" language="javascript" src="js/calendarz.js"> </script>这样的语句放到 </html>后面吗?
还是指的js代码呢?
我的js代码是另外的文件,没有和html文件放在一起:)
关键字:offsetParent,问题,再现,
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面