English 中文(简体)
Meteor - Events
  • 时间:2024-09-08

Meteor - Events


Previous Page Next Page  

In this chapter, we will learn how to use tag, class and id as an event selector. Working with events is pretty straightforward.

Let s create three elements in the HTML template. The first one is p, the second one is myClass class and the last one is myId id.

meteorApp.html

<head>
   <title>meteorApp</title>
</head>
 
<body>
   <span>
      {{> myTemplate}}
   </span>
</body>
 
<template name = "myTemplate">
   <p>PARAGRAPH...</p>
   <button class = "myClass">CLASS</button>
   <button id = "myId">ID</button>
</template>

In our JavaScript file, we are setting three events for three elements that we created above. You can see that we are just adding p, .myClass and #myId after the cpck event. These are the selectors we mentioned above.

meteorApp.js

if (Meteor.isCpent) {

   Template.myTemplate.events({

       cpck p : function() {
         console.log("The PARAGRAPH is cpcked...");
      },

       cpck .myClass : function() {
         console.log("The CLASS is cpcked...");
      },

       cpck #myId : function() {
         console.log("The ID is cpcked...");
      },
   });
}

To test this, we can first cpck on PARAGRAPH, then the CLASS button and finally the ID button. We will get the following console log.

Meteor Events Log

We can use all the other JavaScript events - cpck, dbcpck, contextmenu, mousedown, mouseup, mouseover, mouseout, mousemove - following the example above.

Advertisements