简体   繁体   English

Polymer 不允许在 Chrome 中设置内部元素样式和传播事件

[英]Polymer doesn't allow to style inner elements and propagate events in Chrome

I've started using Polymer in these weeks.这几周我开始使用 Polymer。 I'm attempting to create a paper-submit element which includes a button and a paper-button , which, once pressed, triggers a hidden button's click event.我正在尝试创建一个paper-submit元素,其中包括一个按钮和一个paper-button ,一旦按下,就会触发隐藏按钮的点击事件。

This works fine in Firefox: the paper-button gets styled correctly (using a class) and the click event triggers the button's click event and the containing form triggers the submit one.这在 Firefox 中工作正常: paper-button的样式正确(使用类),点击事件触发按钮的点击事件,包含表单触发提交事件。

On Chrome though, the paper-button ignores the style I've set (see example: no background color change) and it seems to stop propagating the button's click event.但是,在 Chrome 上,纸按钮忽略了我设置的样式(参见示例:背景颜色没有变化)并且它似乎停止传播按钮的点击事件。 The result is an unstyled paper-button which triggers nothing.结果是一个无样式的纸按钮,它什么也不会触发。

This is the source:这是来源:

CSS style, linked in the main page: CSS 样式,链接在主页面:

paper-button.blue {
  color: #FFFFFF;
  background-color: #2196F3; }

paper-submit polymer element:纸提交聚合物元素:

<link href="../polymer/polymer.html" rel="import">
<link href="../paper-button/paper-button.html" rel="import">

<polymer-element name="paper-submit" attributes="bClass raised recenteringTouch fill" role="button">
  <template>
    <style>
      #submit-button {
        display: none;
      }
    </style>
    <paper-button id="button" class="{{bClass}}" raised="{{raised}}" recenteringTouch="{{recenteringTouch}}" role="button">
      <content></content>
    </paper-button>

    <button type="submit" id="submit-button"></button>
  </template>
  <script>
    Polymer({
      publish: {
        bClass: '',
        raised: false,
        recenteringTouch: false,
        fill: true
      },

      ready: function () {
        var submit = this.$['submit-button'];
        var button = this.$.button;
        button.addEventListener('click', function (ev) {
          submit.click();
        });
      }
    });
  </script>
</polymer-element>

Note: I've tried inserting a simple button inside the paper-input element to test if it was something related to the JavaScript's click function, but it's not: same result.注意:我已经尝试在paper-input元素中插入一个简单的按钮来测试它是否与 JavaScript 的点击功能相关,但它不是:相同的结果。

This is how I call the element:这就是我调用元素的方式:

<paper-submit bClass="blue" role="button" tabindex="0">Click me!</paper-submit>

The element is inside a form with some mandatory fields: on Firefox they trigger the error, on Chrome nothing happens.该元素位于具有一些必填字段的表单内:在 Firefox 上它们触发错误,在 Chrome 上没有任何反应。

May you please help me?你能帮帮我吗?

There is another approach: Declarative event mapping还有另一种方法: 声明性事件映射

<paper-button id="button" on-click="{{click_submit}}" ...

  click_submit: function () {
    var submit = this.$['submit-button'];
      submit.click();
  }

As far as the css goes, if you are outside the shadow boundaries, you need to pierce them to override them...就css而言,如果您在阴影边界之外,则需要刺穿它们以覆盖它们...

body /deep/ paper-button {
  ...
}

Here is a codepen with an example这是一个带有示例的代码笔

Not sure why you have to use a new element in this case despite what you said in the comments.尽管您在评论中说了些什么,但不确定为什么在这种情况下必须使用新元素。 You should be able to do the styling & form submit either way.您应该能够以任何一种方式进行样式和表单提交。 I probably just don't have context on your project so I apologize if I am not correct in this.我可能只是没有关于您的项目的背景信息,所以如果我在这方面不正确,我深表歉意。

For some inspiration using 'paper forms' also check out paper-form-on-fire对于使用“纸质表格”的一些灵感,还可以查看paper-form-on-fire

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

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