- 流星教程
- 流星 - 主页
- 流星 - 概述
- Meteor - 环境设置
- Meteor - 首次应用
- 流星 - 模板
- 流星 - 收藏
- 流星 - 形式
- 流星 - 活动
- 流星 - 会话
- 流星 - 追踪器
- 流星 - 套餐
- Meteor - 核心 API
- 流星 - 检查
- 流星 - 烈焰
- Meteor - 计时器
- 流星-EJSON
- 流星 - HTTP
- 流星 - 电子邮件
- 流星 - 资产
- 流星 - 安全
- 流星 - 排序
- 流星 - 账户
- Meteor - 方法
- 流星-Package.js
- Meteor - 发布和订阅
- 流星 - 结构
- 流星 - 部署
- Meteor - 在移动设备上运行
- 流星 - 待办事项应用程序
- Meteor - 最佳实践
- 流星有用资源
- 流星 - 快速指南
- 流星 - 有用的资源
- 流星 - 讨论
流星 - 活动
在本章中,我们将学习如何使用tag、class和id作为事件选择器。处理事件非常简单。
让我们在 HTML 模板中创建三个元素。第一个是p,第二个是myClass类,最后一个是myId id。
流星App.html
<head>
<title>meteorApp</title>
</head>
<body>
<div>
{{> myTemplate}}
</div>
</body>
<template name = "myTemplate">
<p>PARAGRAPH...</p>
<button class = "myClass">CLASS</button>
<button id = "myId">ID</button>
</template>
在我们的 JavaScript 文件中,我们为上面创建的三个元素设置三个事件。您可以看到我们只是在单击事件之后添加了p、.myClass和#myId。这些是我们上面提到的选择器。
流星App.js
if (Meteor.isClient) {
Template.myTemplate.events({
'click p': function() {
console.log("The PARAGRAPH is clicked...");
},
'click .myClass': function() {
console.log("The CLASS is clicked...");
},
'click #myId': function() {
console.log("The ID is clicked...");
},
});
}
为了测试这一点,我们可以首先单击PARAGRAPH,然后单击CLASS按钮,最后单击ID按钮。我们将得到以下控制台日志。
我们可以使用所有其他 JavaScript 事件 - click、dbclick、contextmenu、mousedown、mouseup、mouseover、mouseout、mousemove - 按照上面的示例。