2011-09-15 51 views
4

上我嘗試extjs4標籤上添加點擊事件,但沒有工作如何添加單擊事件的ExtJS 4標籤

Ext.onReady(function() { 

    var ResetLabel = new Ext.form.Label({ 
     id:'ResetLabel', 
     text: 'click it', 
     renderTo : document.body         

    }); 
    alert(Ext.getCmp('ResetLabel').id); 

    Ext.getCmp('ResetLabel').on('click',function(){ 

     alert("message"); 
    }); 

}); 

如何標籤上添加事件?

+0

是否有Javascript錯誤?你可以粘貼所有的代碼,而不僅僅是幾行? –

+0

我描述的代碼,也沒有與單擊事件,並沒有給任何錯誤工作 – jayesh

回答

5

這個代碼在ExtJS的4

Ext.onReady(function() { 

    var ResetLabel = new Ext.form.Label({ 
     id:'ResetLabel', 
     text: 'click it', 
     renderTo : document.body         

    }); 
    alert(Ext.getCmp('ResetLabel').getEl()); 

    Ext.getCmp('ResetLabel').getEl().on('click',function(){ 

     alert("message"); 
    }); 

}); 
3

試試這個:

Ext.onReady(function() { 

var ResetLabel = new Ext.form.Label({ 
    id:'ResetLabel', 
    text: 'click it', 

listeners: { 
    click: function(){ 
      alert("message"); 
     } 
}, 

    renderTo : document.body         

}); 

alert(Ext.getCmp('ResetLabel').id); 


}); 
+0

這段代碼不工作,我使用Extjs4 – jayesh

3
{ xtype: 'label', listeners: { element: 'el', click: function() { alert(); } } } 
+0

你可以解釋你的代碼進一步..?可能有幫助... – NREZ

0

沒錯不爲我工作之一,嘗試了所有的例子...

工作

檢查,這工作

var ResetLabel = new Ext.form.Label({ 
     id:'ResetLabel', 
     text: 'click it' 

    }); 
    Ext.onReady(function() { 
     Ext.getCmp('ResetLabel').getEl().on('click',function(){ 
      alert("message"); 
     }); 
    }); 

我將ResetLabel添加到我的面板。

0

我喜歡較短,以獲得更快的想法:

// Adding abcent label event through its dom-structure: 
myLabel.getEl().on(
    "click", 
    onClickMyLabel); 
1

我工作的ExtJS的3.4之上的舊代碼庫和我下面的工作。我想它應該適用於更高版本。

new Ext.form.Label({ 
    "html": "Halp!", 
    "listeners": { 

     /* We are going to assing the click event right after the element has rendered */ 
     "afterrender": function() { 

      this.getEl().on("click", function() { 
       console.log("Clicked!"); 
      }); 
     } 
    } 
});