我把JS类实例化后,把一个方法做为event添加给一个DIV,为什么它就不能访问类中的属性了

打字员 2008-11-04 08:58:07
本人一个菜菜,刚学写JS类.望各位大哥多多指点!!!


<!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=gb2312" />
<title>无标题文档</title>
<style type="text/css" rel="sheetstyle" />
*{padding:0px;margin:0px;}
img{border:0px;}
ul{list-style:none;}
body{
text-align:center;
font-size:12px;
color:#222;
}
a:link,a:visited{text-decoration:none;color:#000;}
a:hover,a:active{text-decoration:underline;color:#8b0000;}
.bodyer{
width:840px;height:586px;
position:relative;
border-left:2px #8b0000 solid;
border-right:2px #8b0000 solid;
overflow:hidden;
margin:0px auto;
}
.button{
width:20px;height:60px;
background:#8b0000;
position:absolute;left:800px;top:40px;
padding:12px 8px;
line-height:30px;
color:#fff;
font-size:14px;
font-weight:bolder;
cursor:pointer;
}
</style>
<script language="javascript" type="text/javascript">
var DIYViewBox=function(TitleObjId,ContObjId,ContObjH,ContObjW,ContObjL,ContObjT)
{
this.ContOldH=ContObjH;
this.ContOldW=ContObjW;
this.ContNewH=0;
this.ContNewW=0;
this.ContOldL=ContObjL;
this.ContOldT=ContObjT;
this.ContNewL=0;
this.ContNewT=0;
this.TitleObj=null;
this.ContObj=null;
this.ISIE=document.all?true:false

function GetObj(ObjName)
{
if(typeof(ObjName)=="string")
{
if(document.getElementById){return document.getElementById(ObjName)}
else{return document.all.ObjName}
}
else
{
return ObjName
}
}
this.AddEvent=function(EventParent,EventType,EventFun)
{
if(GetObj(EventParent).attachEvent)
{GetObj(EventParent).attachEvent("on"+EventType,EventFun)}
if(GetObj(EventParent).addEventListener)
{GetObj(EventParent).addEventListener(EventType,EventFun,false)}
}
this.RemoveEvent=function(EventParent,EventType,EventFun)
{
if(GetObj(EventParent).detachEvent)
{GetObj(EventParent).detachEvent("on"+EventType,EventFun)}
if(GetObj(EventParent).removeEventListener)
{GetObj(EventParent).removeEventListener(EventType,EventFun,false)}
}
this.CreateBox=function()
{
this.TitleObj=GetObj(TitleObjId)
var ParentObj=this.TitleObj.parentNode;
var CreateObj=document.createElement("div");
CreateObj.id=ContObjId;
CreateObj.setAttribute("id",ContObjId)
ParentObj.appendChild(CreateObj)
this.ContObj=GetObj(ContObjId)
this.ContObj.style.position="absolute";
this.ContObj.style.display="block";
this.ContObj.style.height=this.ISIE?this.ContOldH:this.ContOldH+"px";
this.ContObj.style.width=this.ISIE?this.ContOldW:this.ContOldW+"px";
this.ContObj.style.left=this.ISIE?this.ContOldL:this.ContOldL+"px";
this.ContObj.style.top=this.ISIE?this.ContOldT:this.ContOldT+"px";
this.ContObj.style.border="#000 solid 1px"
this.ContObj.innerHTML="danica"
//alert()
this.AddEvent(this.TitleObj,"click",this.ViewBox)
}
this.ViewBox=function()
{
var DIS=(this.ContObj.style.display="block")?true:false
if(DIS){this.ContObj.style.display="none"}
else{this.ContObj.style.display="display"}
}
}
</script>
</head>
<body>
<div class="bodyer">
<div class="button" id="Title">日记</div>
<script language="javascript" type="text/javascript">
Box1=new DIYViewBox("Title","Cont",400,400,20,40);
Box1.CreateBox();
</script>
</div>
</body>
</html>


我点击"日记".FF中就报错,说this.ContObj未定义.



...全文
164 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
打字员 2008-11-05
  • 打赏
  • 举报
回复
谢谢,学习中...
看来有不懂的找人请教还是比自己闭门造车好多了.
我自己在网上找了半天相关的东西.结果还是一头雾水.
-_-
不悲不喜 2008-11-04
  • 打赏
  • 举报
回复
neo_yoho 2008-11-04
  • 打赏
  • 举报
回复
你可以看下下面的帖子
http://topic.csdn.net/u/20081104/08/55e3c0fe-711e-406e-bb56-9deecd91cac9.html
打字员 2008-11-04
  • 打赏
  • 举报
回复
大哥,能不能说清楚一点.我对JS类是还是一知半解.
rjzou2006 2008-11-04
  • 打赏
  • 举报
回复
朋友,要明白在this实际指向谁.


<!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=gb2312" />
<title>无标题文档</title>
<style type="text/css" rel="sheetstyle" />
*{padding:0px;margin:0px;}
img{border:0px;}
ul{list-style:none;}
body{
text-align:center;
font-size:12px;
color:#222;
}
a:link,a:visited{text-decoration:none;color:#000;}
a:hover,a:active{text-decoration:underline;color:#8b0000;}
.bodyer{
width:840px;height:586px;
position:relative;
border-left:2px #8b0000 solid;
border-right:2px #8b0000 solid;
overflow:hidden;
margin:0px auto;
}
.button{
width:20px;height:60px;
background:#8b0000;
position:absolute;left:800px;top:40px;
padding:12px 8px;
line-height:30px;
color:#fff;
font-size:14px;
font-weight:bolder;
cursor:pointer;
}
</style>
<script language="javascript" type="text/javascript">
var DIYViewBox=function(TitleObjId,ContObjId,ContObjH,ContObjW,ContObjL,ContObjT)
{
this.ContOldH=ContObjH;
this.ContOldW=ContObjW;
this.ContNewH=0;
this.ContNewW=0;
this.ContOldL=ContObjL;
this.ContOldT=ContObjT;
this.ContNewL=0;
this.ContNewT=0;
this.TitleObj=null;
this.ContObj=null;
this.ISIE=document.all?true:false
_self = this;
function GetObj(ObjName)
{
if(typeof(ObjName)=="string")
{
if(document.getElementById){return document.getElementById(ObjName)}
else{return document.all.ObjName}
}
else
{
return ObjName
}
}
this.AddEvent=function(EventParent,EventType,EventFun)
{
if(GetObj(EventParent).attachEvent)
{GetObj(EventParent).attachEvent("on"+EventType,EventFun)}
if(GetObj(EventParent).addEventListener)
{GetObj(EventParent).addEventListener(EventType,EventFun,false)}
}
this.RemoveEvent=function(EventParent,EventType,EventFun)
{
if(GetObj(EventParent).detachEvent)
{GetObj(EventParent).detachEvent("on"+EventType,EventFun)}
if(GetObj(EventParent).removeEventListener)
{GetObj(EventParent).removeEventListener(EventType,EventFun,false)}
}
this.CreateBox=function()
{
this.TitleObj=GetObj(TitleObjId)
var ParentObj=this.TitleObj.parentNode;
var CreateObj=document.createElement("div");
CreateObj.id=ContObjId;
CreateObj.setAttribute("id",ContObjId)
ParentObj.appendChild(CreateObj)
this.ContObj=GetObj(ContObjId)
this.ContObj.style.position="absolute";
this.ContObj.style.display="block";
this.ContObj.style.height=this.ISIE?this.ContOldH:this.ContOldH+"px";
this.ContObj.style.width=this.ISIE?this.ContOldW:this.ContOldW+"px";
this.ContObj.style.left=this.ISIE?this.ContOldL:this.ContOldL+"px";
this.ContObj.style.top=this.ISIE?this.ContOldT:this.ContOldT+"px";
this.ContObj.style.border="#000 solid 1px"
this.ContObj.innerHTML="danica"
//alert()
this.AddEvent(this.TitleObj,"click",this.ViewBox)
}
this.ViewBox=function()
{
var DIS=(_self.ContObj.style.display="block")?true:false
if(DIS){_self.ContObj.style.display="none"}
else{_self.ContObj.style.display="display"}
}
}
</script>
</head>
<body>
<div class="bodyer">
<div class="button" id="Title">日记</div>
<script language="javascript" type="text/javascript">
Box1=new DIYViewBox("Title","Cont",400,400,20,40);
Box1.CreateBox();
</script>
</div>
</body>
</html>

87,996

社区成员

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

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