javascript - 在 Chrome 中点击时 polymer 纸对话框不显示

标签 javascript jquery google-chrome polymer polymer-1.0

我正在使用最新的polymer starter kit 2.0 ,我在下面有这段代码,并尝试打开对话框,但在 chrome 中没有任何反应(它在 Firefox 中工作得很好):

<paper-fab icon="zoom-in" onclick="dialog.open()"></paper-fab>
<paper-dialog id="dialog">
   <h3>text</h3>
   <div class="buttons">
     <paper-button dialog-confirm>Close</paper-button>
   </div>
</paper-dialog>

我已导入

<link rel="import" href="../bower_components/paper-dialog/paper-dialog.html">
<link rel="import" href="../bower_components/paper-dialog-behavior/paper-dialog-behavior.html">

在“my-app.html”中

并安装:

bower install --save PolymerElements/paper-dialog
bower install --save PolymerElements/paper-dialog-behavior

enter image description here

关于如何解决这个问题有什么想法吗?

左 - Firefox |右 - Chrome/opera

更新:

我有默认的polymer starter kit 2.0项目中添加了一些元素到页面。

我正在使用polymer serve将页面转换到 localhost:8080

我刚刚创建了另一个页面:src/my-testpage.html

并添加<my-testpage name="testpage"></my-testpage> 熨烫“my-app.html”中的页面

初始代码:

<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="shared-styles.html">

<dom-module id="my-testpage">
  <template>
    <style include="shared-styles">
      :host {
        display: block;
        padding: 10%;
        padding-left: 20%;
        padding-right: 20%;
      }
    </style>

    <paper-fab icon="zoom-in" onclick="dialog.open()"></paper-fab>
    <paper-dialog id="dialog">
       <h3>text</h3>
       <div class="buttons">
         <paper-button dialog-confirm>Close</paper-button>
       </div>
    </paper-dialog>
  </template>

  <script>
    Polymer({
      is: 'my-testpage'
    });
  </script>
</dom-module>

结果:适用于 Firefox 和 EDGE,不适用于基于 chromium 的浏览器 - Chrome/Opera:

控制台显示:

testpage:1 
Uncaught ReferenceError: dialog is not defined
  onclick @ testpage:1

建议的解决方案 1:

请改用:onclick="document.getElementById('dialog').open()

<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="shared-styles.html">

<dom-module id="my-testpage">
  <template>
    <style include="shared-styles">
      :host {
        display: block;
        padding: 10%;
        padding-left: 20%;
        padding-right: 20%;
      }
    </style>

    <paper-fab icon="zoom-in" onclick="document.getElementById('dialog').open()"></paper-fab>
    <paper-dialog id="dialog">
       <h3>text</h3>
       <div class="buttons">
         <paper-button dialog-confirm>Close</paper-button>
       </div>
    </paper-dialog>
  </template>

  <script>
    Polymer({
      is: 'my-testpage'
    });
  </script>
</dom-module>

结果:适用于 Firefox 和 EDGE,不适用于基于 chromium 的浏览器 - Chrome/Opera: 控制台显示:

testpage:1 
Uncaught TypeError: Cannot read property 'open' of null
  onclick @ 

建议的解决方案 2:

<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="shared-styles.html">

<dom-module id="my-testpage">
  <template is="dom-bind" id="t">
    <style include="shared-styles">
      :host {
        display: block;
        padding: 10%;
        padding-left: 20%;
        padding-right: 20%;
      }
    </style>
       <paper-fab icon="zoom-in" on-tap="openDialog"></paper-fab>
       <paper-dialog id="dialog">
          <h3>text</h3>
          <div class="buttons">
            <paper-button dialog-confirm>Close</paper-button>
          </div>
       </paper-dialog>
    <script>
       var t = document.getElementById('t');
       t.openDialog = function() {
         t.$.dialog.open();
       };
    </script>

  </template>

  <script>
    Polymer({
      is: 'my-testpage'
    });
  </script>
</dom-module>

结果:

    .polymer-micro.html.js:265:1
    [my-testpage::_createEventHandler]: listener method `openDialog` not defined

最佳答案

看来您可能依赖 named access on the Window object ,其中 should be avoided .

相反,请使用document.getElementById('dialog'),如以下代码段所示:

<head>
  <base href="https://polygit.org/polymer+1.9.3/components/">
  <script src="webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="import" href="polymer/polymer.html">
  <link rel="import" href="iron-icons/iron-icons.html">
  <link rel="import" href="paper-button/paper-button.html">
  <link rel="import" href="paper-fab/paper-fab.html">
  <link rel="import" href="paper-dialog/paper-dialog.html">
</head>
<body>
  <paper-fab icon="zoom-in" onclick="document.getElementById('dialog').open()"></paper-fab>
  <paper-dialog id="dialog">
     <h3>text</h3>
     <div class="buttons">
       <paper-button dialog-confirm>Close</paper-button>
     </div>
  </paper-dialog>
</body>

codepen

或者,您可以使用 tap 处理程序,如以下代码段所示:

<head>
  <base href="https://polygit.org/polymer+1.9.3/components/">
  <script src="webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="import" href="polymer/polymer.html">
  <link rel="import" href="iron-icons/iron-icons.html">
  <link rel="import" href="paper-button/paper-button.html">
  <link rel="import" href="paper-fab/paper-fab.html">
  <link rel="import" href="paper-dialog/paper-dialog.html">
</head>
<body unresolved>
  <template is="dom-bind" id="t">
    <paper-fab icon="zoom-in" on-tap="openDialog"></paper-fab>
    <paper-dialog id="dialog">
       <h3>text</h3>
       <div class="buttons">
         <paper-button dialog-confirm>Close</paper-button>
       </div>
    </paper-dialog>
  </template>
  <script>
    var t = document.getElementById('t');
    t.openDialog = function() {
      t.$.dialog.open();
    };
  </script>
</body>

codepen

更新您的 polymer 元素代码应如下所示:

<dom-module id="my-testpage">
  <template>
    ...
    <paper-fab icon="zoom-in" on-tap="openDialog"></paper-fab>
    <paper-dialog id="dialog">
        <h3>text</h3>
        <div class="buttons">
          <paper-button dialog-confirm>Close</paper-button>
        </div>
    </paper-dialog>
  </template>

  <script>
    Polymer({
      is: 'my-testpage',

      openDialog: function() {
        this.$.dialog.open();
      }
    });
  </script>
</dom-module>

关于javascript - 在 Chrome 中点击时 polymer 纸对话框不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39594426/

相关文章:

javascript - Javascript 中的提升/返回变量

html - 如何去除边框?

Javascript canvas.toDataUrl() 将图片发送到新窗口中的 <img> 元素

javascript - jQuery:如何检查元素是否存在并更改 css 属性

javascript - 替换字符串中的所有 <p></p> 标签

javascript - 输入类型文件的jquery显示值到其他元素

google-chrome - chrome Typescript 界面中的意外标识符

jquery - 为什么 jQuery hide()/show() 会破坏 Chrome 中的 HTML 选择?

javascript - 数据属性检索和解析 javascript

jquery - 使用 jquery 遍历表格单元格