文章出處
文章列表
可以在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');
文章列表
全站熱搜