2014-06-28 124 views
2

似乎無法獲取<paper-tabs><paper-tab>來觸發點擊事件。有趣的是,如果我只是抓住元素並在Chrome的開發工具上註冊事件監聽器,它就可以正常工作。但不是在我的應用程序代碼:聚合物紙標籤點擊事件?

// app.html 
<paper-tabs selected="0" class="bottom indent" style="width: 200px;" self-end> 

    <paper-tab id="one">ONE</paper-tab> 
    <paper-tab id="two">TWO</paper-tab> 

</paper-tabs> 

// app.js 
window.addEventListener('polymer-ready', function(e) { 
    var tabs = { 
     one: document.querySelector('#one'), 
     two: document.querySelector('#two') 
    }; 

    tabs.one.addEventListener('click', function(e) { 
     console.log(e, this); 
    }); 

    // !! This logs the expected object !! 
    console.log(tabs); 
} 

我在這裏忽略了什麼嗎?我確實有其他組件(紙張滑塊)與事件監聽器正常工作,但不是標籤。

回答

1

工作時,我試了一下:http://jsbin.com/sohik/1/edit

是否有失敗特定瀏覽器?

請注意,只有在訪問自定義API時才需要等待polymer-ready。否則,可以定位元素本身,並且可以成功添加事件偵聽器。

0

試試這個:

$(document).on('click', 'paper-tab', function() { 
//function code 
}); 

不要擔心試圖添加事件偵聽器聚合物被加載後 - 只需使用$(document).on(EVENT, TYPE, FUNCTION(){CODE});

0

我這樣做,它的工作相當不錯!

在我的index.html

<paper-tabs id="mainTab" selected="{{selectedtab}}" noink class="bottom flex" style="width= 300px;" on-iron-select="onTabSelect"> your tabs here </paper-tabs> 

在我app.js

app.onTabSelect = function(event, details){  
console.log(event.target.selected); }; 

就是這樣。

0

例如

聚合物1.0

<dom-module id="example-cell"> 
    <div class="call-button" on-click="hello"> 
     example 
    </div> 
    Polymer({ 
     is: 'example-cell', 
     hello: function(){ 
      alert("hi"); 
     } 
</dom-module> 
0

下面的示例的工作原理:

HTML:

<paper-tabs selected="{{aType}}"> 
    <template is="dom-repeat" items="[[activityTypes]]"> 
     <paper-tab on-tap="onAtSelect">[[item.name]]</paper-tab> 
    </template> 
</paper-tabs> 

使用Javascript:

onAtSelect: function(e) { 
    console.log(e.model.item); 
}