简体   繁体   中英

How to add click event on Extjs 4 label

I try to add click event on label in extjs4 but not working

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

});

How to add event on a label?

this code is working in 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");
    });

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

try this:

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


}); 

Yep doesn't work for me either, tried all the examples...

check that, this worked

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

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

I am adding the ResetLabel to my panel.

I'm working on an old code-base on top of ExtJS 3.4 and the following worked for me. I guess it should work for higher versions as well.

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!" );
           });
       }
    }
});

I like it shorter to get the idea quicker:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM