- Ext.js 教程
- Ext.js - 首页
- Ext.js - 概述
- Ext.js - 环境设置
- Ext.js - 命名规范
- Ext.js - 架构
- Ext.js - 第一个程序
- Ext.js - 类系统
- Ext.js - 容器
- Ext.js - 布局
- Ext.js - 组件
- Ext.js - 拖放
- Ext.js - 主题
- Ext.js - 自定义事件和监听器
- Ext.js - 数据
- Ext.js - 字体
- Ext.js - 样式
- Ext.js - 绘制
- Ext.js - 本地化
- Ext.js - 可访问性
- Ext.js - 调试代码
- Ext.js - 方法
- Ext.js 有用资源
- Ext.js - 问答
- Ext.js - 快速指南
- Ext.js - 有用资源
- Ext.js - 讨论
Ext.js - 自定义事件和监听器
事件是在类发生某些事情时触发的。例如,当按钮被点击或元素渲染之前/之后。
编写事件的方法
- 使用监听器内置事件
- 稍后附加事件
- 自定义事件
使用监听器内置事件
Ext JS 提供了 listener 属性用于在 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 = "https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/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 = "https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/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>
稍后附加事件
在之前编写事件的方法中,我们在创建元素时在 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 = "https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/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 = "https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0.0/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);
广告