ITeye博客

this使用帮助

czllfy · 2008-11-15 16:29 · js · Web前端 · Markdown

[code="java"] New Document [/code] 我想让div的onclick事件输出'division element',怎么处理 我对this还是不清楚呀

评论(5)

  1. #5 楼 · tidus · 2008-11-17 11:40

    //事件绑定时,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

  2. #4 楼 · afcn0 · 2008-11-16 19:49

    给你弄错了,是这样的,你在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. #3 楼 · tidus · 2008-11-16 18:15

    //alert(obj.m_Text); alert(new JSClass().m_Text);

  4. #2 楼 · czllfy · 2008-11-16 01:05

    afcn0 writes:

    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>
    
    
    
    
    
  5. #1 楼 · afcn0 · 2008-11-15 19:44

    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);

    // }

    };

评论功能维护中,当前仅展示历史评论。