document.getElementById为什么找不到对象

soft113 2007-11-16 03:30:59
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<style type="text/css">
#toolTip{width:205px;}
#toolTip{display:none;}
#toolTip{position:absolute;color:#333;}
#tip_top{background:url(images/tip_top.gif) no-repeat top center;height:20px;}
#tip_middle{background:url(images/tip_middle.gif) repeat-y center;padding:0 10px;}
#tip_bottom{background:url(images/tip_bottom.gif) no-repeat top center;height:20px;}
</style>
<script type="text/javascript" language="javascript">
toolTip = document.createElement("DIV");
toolTip.id = "toolTip";
tip_top = document.createElement("DIV");
tip_top.id = "tip_top";
tip_middle = document.createElement("DIV");
tip_middle.id = "tip_middle";
tip_bottom = document.createElement("DIV");
tip_bottom.id = "tip_bottom";
toolTip.appendChild(tip_top);
toolTip.appendChild(tip_middle);
toolTip.appendChild(tip_bottom);

title_content = "";
cStyle = toolTip.style;
cStyle.position = "absolute";
//for(i = 0; i < document.all.length; i++){
//alert(document.all(i).tagName);
//}
var _tempdoc= document.getElementById("container");
//alert(_tempdoc);
_tempdoc.appendChild(toolTip);

function showTooltips(evt)
{
evt = window.event?window.event:evt;
srcElem = evt.srcElement?evt.srcElement:evt.target;
if(srcElem.tagName.toUpperCase()=="A" && srcElem.title!=""){
window.title_content = srcElem.title;
srcElem.title = "";
tip_middle.innerHTML = window.title_content;
pos = findPosition(srcElem);
x=pos[0];y=pos[1];
cStyle.left = x+"px";
cStyle.top = y+25+"px";
cStyle.display = "block";
}
}

function findPosition( oLink ) {
if( oLink.offsetParent ) {
for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {
posX += oLink.offsetLeft;
posY += oLink.offsetTop;
}
return [ posX, posY ];
} else {
return [ oLink.x, oLink.y ];
}
}

function hideTooltips(evt)
{
evt = window.event?window.event:evt;
srcElem = evt.srcElement?evt.srcElement:evt.target;
srcElem.title= window.title_content;
window.title_content = "";
cStyle.display = "none";
}
document.onmouseover = showTooltips;
document.onmouseout = hideTooltips;
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<div id="container">
<div id="header">
<h1>Header</h1>

<!-- end #container --></div>
</form>
</body>
</html>
...全文
985 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
菠萝味苹果 2011-05-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liveforever_1981 的回复:]
var _tempdoc= document.getElementById( "container ");
?
因为你还没载入标签呢 当然找不到

写在 后面
或 添进 载入后的事件
[/Quote]
你说的不对吧! JS写在<head>里不是很正常? 我这样写很正常的找到了!
liveforever_1981 2007-11-16
  • 打赏
  • 举报
回复
太牛了 [无满意答案结贴]
真行!
lion98 2007-11-16
  • 打赏
  • 举报
回复
JS写在</Body>的下面,DIV还没有加载,就去取,肯定取不到,而且你的
DIV不匹配!
rjzou2006 2007-11-16
  • 打赏
  • 举报
回复
ie6打开没有错误?

是哪里错了呢?
liveforever_1981 2007-11-16
  • 打赏
  • 举报
回复
var _tempdoc= document.getElementById( "container ");
?
因为你还没载入标签呢 当然找不到

写在 后面
或 添进 载入后的事件

87,916

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧