简体   繁体   English

聚合物纸标签点击事件?

[英]Polymer paper-tabs click event?

Can't seem to get <paper-tabs> or <paper-tab> to fire click events.似乎无法让<paper-tabs><paper-tab>触发点击事件。 Funny thing is, if I just grab the element and register the event listener on Chrome's dev tools, it works fine.有趣的是,如果我只是获取元素并在 Chrome 的开发工具上注册事件侦听器,它就可以正常工作。 But not so in my app code:但在我的应用程序代码中并非如此:

// 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);
}

Am I overlooking something here?我在这里忽略了什么吗? I do have other components (paper-slider) working correctly with event listener, but not the tabs.我确实有其他组件(纸滑块)与事件侦听器一起正常工作,但不是选项卡。

Worked when I tried it: http://jsbin.com/sohik/1/edit当我尝试它时工作: http : //jsbin.com/sohik/1/edit

Is there a particular browser that is failing?是否有特定的浏览器出现故障?

Note that waiting for polymer-ready is only necessary when accessing custom api.请注意,只有在访问自定义 api 时才需要等待polymer-ready Otherwise, the elements themselves can be located, and event listeners can be added successfully.否则,可以定位元素本身,并且可以成功添加事件侦听器。

Try this:尝试这个:

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

Don't worry about trying to add an event listener after polymer is loaded - just use $(document).on(EVENT, TYPE, FUNCTION(){CODE});不要担心在聚合物加载后尝试添加事件侦听器 - 只需使用$(document).on(EVENT, TYPE, FUNCTION(){CODE});

I did this and it worked pretty well!!我这样做了,而且效果很好!!

in my index.html在我的 index.html

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

and in my app.js在我的 app.js 中

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

That's it.就是这样。

example例子

polymer v1.0聚合物 v1.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>

The following example works:以下示例有效:

Html:网址:

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

Javascript: Javascript:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM