meteor事件
与javaScript选择器相同,不同点在于,使用时加上事件名称和标签名;例如
main.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>
main.js
if (Meteor.isClient) { //此事件是在meteor的客户端发生,并只适用于客户端
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...");
},
});
}
其他jsvaScript事件可以参看点击时间使用方法,均可使用
使用一下方法也可进行事件操作
Template.myTemplate.onCreated(function(){
this.data = new ReactiveVar();
this.data.set('1');
})
Template.myTemplate.helpers({
data(){ return Template.instance().data.get() }
})
Template.myTemplate.events({
'click p': (e,template){
//e为当前事件
//template 可以获取此js中helpers中的方法,例如data;
console.log('===',template.data.get()); // === 1
console.log("The PARAGRAPH is clicked...");
},
'click .myClass': function(){
console.log("The CLASS is clicked...");
},
});
希望可以和大家一起进步。