`
czllfy
  • 浏览: 107145 次
  • 来自: ...
社区版块
存档分类
最新评论

this使用帮助

    博客分类:
  • js
 
阅读更多
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
  <script>
  function JSClass()
   {
     this.m_Text = 'division element';
      this.m_Element = document.createElement('DIV');
      this.m_Element.innerHTML = this.m_Text;
        
      this.m_Element.attachEvent('onclick', this.ToString);
   }
   
   JSClass.prototype.Render = function()
   {
       document.body.appendChild(this.m_Element);
   }     

   JSClass.prototype.ToString = function()
   {
		if(this instanceof JSClass){
			alert(this.m_Text);
		}else{
			alert(obj.m_Text);
		}

	    
   };

  var jc = new JSClass();
   jc.Render(); 
   jc.ToString();
  </script>
 </BODY>
</HTML>


我想让div的onclick事件输出'division element',怎么处理
我对this还是不清楚呀
分享到:
评论
5 楼 tidus 2008-11-17  
//事件绑定时,this指向的是当前对象
//事件触发时,this指向的是window;

帮你up, 继续研究;

实例声明和调用实例方法都没什么可说的,元素的click事件的绑定到了一个实例的方法,那么通过addEventListener绑定到的方法是拷贝过后的,所以this指的是html元素,这个元素没有m_Text属性(m_Text属性是属于JSClass的实例的,即属于jc的),所以点击元素显示undefined,attachEvent绑定的事件会将函数复制到全局,此时this指的是window对象,点击元素也会显示“undefined”。只有在调用jc.ToString()方法是,this指的是jc这个对象,因为jc拥有m_Text,所以能够显示“division element”。 Long Way
4 楼 afcn0 2008-11-16  
给你弄错了,是这样的,你在div上面事件处理函数只能得到div这个对象,而这个对象和你new的对象之间没有关系,必须叫div下面有原来this对象的引用才可以找到
function JSClass()    
{    
  this.m_Text = 'division element';    
   this.m_Element = document.createElement('DIV');    
   this.m_Element.innerHTML = this.m_Text;    
   this.m_Element.thisWrap=this;      
   this.m_Element.onclick=this.ToString;    
}    
    
JSClass.prototype.Render = function()    
{    
    document.body.appendChild(this.m_Element);    
}         
 
JSClass.prototype.ToString = function()    
{    
         alert(this.thisWrap.m_Text);    
 
         
};   
另外,attachEvent上去的函数会丢失事件发生的html对象this.
3 楼 tidus 2008-11-16  
//alert(obj.m_Text);
alert(new JSClass().m_Text);
2 楼 czllfy 2008-11-16  
afcn0 写道
#   function JSClass() 
#    { 
#      this.m_Text = 'division element'; 
#       this.m_Element = document.createElement('DIV'); 
#       this.m_Element.innerHTML = this.m_Text; 
#          
#       this.m_Element.onclick=this.ToString; 
#    } 
#     
#    JSClass.prototype.Render = function() 
#    { 
#        document.body.appendChild(this.m_Element); 
#    }      
#  
#    JSClass.prototype.ToString = function() 
#    { 
#         //if(this instanceof JSClass){ 
#             alert(this.m_Text); 
#        // }else{ 
#       //      alert(obj.m_Text); 
#       //  } 
#  
#          
#    }; 


afcn0 你好
你的方法我测试过发现返回的还是undefined.
附上测试的代码

<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
<script>
function JSClass()  
{  
  this.m_Text = 'division element';  
   this.m_Element = document.createElement('DIV');  
   this.m_Element.innerHTML = this.m_Text;  
       
   this.m_Element.onclick=this.ToString;  
}  
  
JSClass.prototype.Render = function()  
{  
    document.body.appendChild(this.m_Element);  
}       

JSClass.prototype.ToString = function()  
{  
         alert(this.m_Text);  

       
}; 
var jc = new JSClass();   
   jc.Render();    
   jc.ToString();   
</script>
 </BODY>
</HTML>



1 楼 afcn0 2008-11-15  
#   function JSClass() 
#    { 
#      this.m_Text = 'division element'; 
#       this.m_Element = document.createElement('DIV'); 
#       this.m_Element.innerHTML = this.m_Text; 
#          
#       this.m_Element.onclick=this.ToString; 
#    } 
#     
#    JSClass.prototype.Render = function() 
#    { 
#        document.body.appendChild(this.m_Element); 
#    }      
#  
#    JSClass.prototype.ToString = function() 
#    { 
#         //if(this instanceof JSClass){ 
#             alert(this.m_Text); 
#        // }else{ 
#       //      alert(obj.m_Text); 
#       //  } 
#  
#          
#    }; 

相关推荐

Global site tag (gtag.js) - Google Analytics