polymer1.0 event listener

You can listen for events in the listeners object

<x-custom></x-custom>
<dom-module id="x-custom">
  <template>
    <div>I will respond</div>
    <div>to a tap on</div>
    <div>any of my children!</div>
    <div id="special">I am special!</div>
  </template>

  <script>
    Polymer({

      is: 'x-custom',

      listeners: {
        'tap': 'regularTap',
        'special.tap': 'specialTap'
      },

      regularTap: function(e) {
        alert("Thank you for tapping");
      },

      specialTap: function(e) {
        alert("It was special tapping");
      }

    });
  </script>
</dom-module>
 'special.tap': 'specialTap'

This sentence means to assign a tap event to the element whose id is special.

In addition to this way, we can also do this.

<x-custom></x-custom>
<dom-module id="x-custom">
  <template>
    <button id="btn">点击我!</button>
  </template>

  <script>
    Polymer({
      is: 'x-custom',
      attached:function(){
        this.listen(this.$.btn,'click','sayHello');
      },
      sayHello:function(){
        console.log('sayHello');
      }

    });
  </script>
</dom-module>

this.listen(element to add event, event, specific event);

This represents the outermost element, and the id element can be obtained through this.$.

delete event
this.unlisten(this.$.myButton, 'tap', 'onTap');

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325251925&siteId=291194637