ExtJs 专题
专题目录
您的位置:JS框架 > ExtJs 专题 > Ext.js 自定义事件和监听器
Ext.js 自定义事件和监听器
作者:--    发布时间:2019-11-20

事件是在类发生的时候触发的。 例如,当一个按钮被点击或元素被渲染之前/之后。

写事件的方法:

  1. 内置事件使用侦听器
  2. 稍后附加事件
  3. 自定义事件

内置事件使用侦听器

ext js提供了用于在ext js文件中编写事件和自定义事件的侦听器属性。

在ext js中编写侦听器

我们将通过在面板中添加listen属性来将监听器添加到上一个程序中,如下所示:

<!doctype html>
<html>
   <head>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/classic/theme-neptune/resources/theme-neptune-all.css" rel="stylesheet" />
      <script type="text/javascript" src="/attachements/w3c/ext-all.js"></script>
      <script type="text/javascript">
         ext.onready(function(){
            ext.create('ext.button', {
               renderto: ext.getelementbyid('helloworldpanel'),
               text: 'my button',
               listeners: {
                  click: function() {
                     ext.messagebox.alert('alert box', 'button is clicked');	
                  }
               }
            });
         });
      </script> 
   </head>
   <body>
      <p> please click the button to see event listener </p>
      <div id = 'helloworldpanel' />   <!--  panel will be rendered here-- >
   </body>
</html>

这会产生以下结果:

这样我们可以在listeners属性中写多个事件。

同一个侦听器中的多个事件

<!doctype html>
<html>
   <head>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/classic/theme-neptune/resources/theme-neptune-all.css" rel="stylesheet" />
      <script type="text/javascript" src="/attachements/w3c/ext-all.js"></script>
      <script type="text/javascript">
         ext.onready(function(){
            ext.get('tag2').hide()
            ext.create('ext.button', {
               renderto: ext.getelementbyid('helloworldpanel'),
               text: 'my button',
               listeners: {
                  click: function() {
                     this.hide();
                  },
                  hide: function() {
                     ext.get('tag1').hide();
                     ext.get('tag2').show();
                  }
               }
            });
         });           
      </script> 
   </head>
   <body>
   <div id = "tag1">please click the button to see event listener.</div>
   <div id = "tag2">the button was clicked and now it is hidden.</div>
   <div id = 'helloworldpanel' />   <!--  panel will be rendered here-- >
   </body>
</html>

稍后再附加事件

在前面的写事件的方法中,我们在创建元素时在侦听器中写入事件。

其他方式是附加事件在as:

<!doctype html>
<html>
   <head>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/classic/theme-neptune/resources/theme-neptune-all.css" rel="stylesheet" />
      <script type="text/javascript" src="/attachements/w3c/ext-all.js"></script>
      <script type="text/javascript">
         ext.onready(function(){
            var button = ext.create('ext.button', {
               renderto: ext.getelementbyid('helloworldpanel'),
            text: 'my button'
            });

            // this way we can attach event to the button after the button is created.
            button.on('click', function() {
               ext.messagebox.alert('alert box', 'button is clicked');
            });
         });
      </script> 
   </head>
   <body>
      <p> please click the button to see event listener </p>
      <div id = 'helloworldpanel' />   <!--  panel will be rendered here-- >
   </body>
</html>

自定义事件

我们可以在ext js中编写自定义事件,并使用fireevent方法触发事件,下面的示例解释了如何编写自定义事件。

<!doctype html>
<html>
   <head>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/classic/theme-neptune/resources/theme-neptune-all.css" rel="stylesheet" />
      <script type="text/javascript" src="/attachements/w3c/ext-all.js"></script>
      <script type="text/javascript">
         ext.onready(function(){
            var button = ext.create('ext.button', {
               renderto: ext.getelementbyid('helloworldpanel'),
               text: 'my button',
               listeners: {
                  myevent: function(button) {
                     ext.messagebox.alert('alert box', 'my custom event is called');
                  }
               }
            });
            ext.defer(function() {
               button.fireevent('myevent');
            }, 5000);
         }); 
      </script> 
   </head>
   <body>
      <p> the event will be called after 5 seconds when the page is loaded. </p>
      <div id = 'helloworldpanel' />   <!--  panel will be rendered here-- >
   </body>
</html>

一旦页面被加载并且文档准备就绪,ui页面与按钮将出现,并且我们在5秒后触发事件文档准备就绪,警报框将在5秒后出现。

这里我们写了自定义事件\'myevent\',我们将事件触发为button.fireevent(eventname);

这些是在ext js中编写事件的三种方式。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册