文章出處

可以在listeners對象中監聽事件

<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'

這句話的意思是給id為special的元素單獨指定一個tap事件。

除了通過這種方式,我們還可以這樣。

<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(要添加事件的元素, 事件, 具體事件);

this表示最外層的元素,通過this.$可以獲取id元素。

刪除事件
this.unlisten(this.$.myButton, 'tap', 'onTap');


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()