2015-07-11 61 views

回答

4

你可以使用「DOM-bind'template(也被稱爲 '自動綁定模板')https://www.polymer-project.org/1.0/docs/devguide/templates.html#dom-bind

<template is="dom-bind" id="app"> 
    //document body 
    <div id="play-button-png" on-click="openVideo"></div> 
</template> 

然後添加功能的模板範圍

var app = document.querySelector('#app'); 
app.openVideo = function() { 
    // do something when clicked 
}; 

編輯:有時在操作任何東西之前,您需要等待模板被綁定。那麼你會等待 'DOM的變化' 事件

app.addEventListener('dom-change', function() { 
    // auto-binding template is ready. 
}); 
1

這裏有另一種解釋https://www.polymer-project.org/1.0/docs/devguide/events

事件監聽器設置

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

註釋事件偵聽器設置

<dom-module id="x-custom"> 
    <template> 
    <button on-tap="handleTap">Kick Me</button> 
    </template> 
    <script> 
    Polymer({ 
     is: 'x-custom', 
     handleTap: function() { 
     alert('Ow!'); 
     } 
    }); 
    </script> 
</dom-module> 
相關問題