【问题标题】:Polymer: Where exactly do I put my Javascript for an Element?Polymer:我究竟应该把我的 Javascript 放在哪里?
【发布时间】:2014-11-08 17:31:36
【问题描述】:

我在普通聚合物元素中加载 Javascript 时遇到问题:

以下是在没有聚合物的情况下运行良好的测试代码:

 <body>
    <style>
    #content {background-color: #f2f2f2;}
    #button {padding: 10px 20px;}
    </style>

    <div id="content">
        <h1 id="title">Title</h1>

        <p>This example uses the addEventListener() method to attach a click event to the document.</p>


        <button id="button">Testbutton</button>
    </div>

    <script>
        function popup2() {
            alert("What's up brow?");
            console.log("deine mudda");
        };

        var button = document.getElementById("button");
        button.addEventListener("click", popup2, false);
    </script>
</script>

</body>

但我想在 Polymer Elements 中使用 Javascript 并尝试了以下插入:

编号1:脚本标签内的Javascript,模板标签之后:

 <polymer-element name="my-element">

    <template>
        <style>
        #content {background-color: #f2f2f2;}
        #button {padding: 10px 20px;}
        </style>

        <div id="content">
            <h1 id="title">Title</h1>

            <p>This example uses the addEventListener() method to attach a click event to the document.</p>


            <button id="button">Testbutton</button>
        </div>

    </template>

    <script>
        Polymer('my-element', {
            popup: function() {
                alert('What the... dude?');
            },

            var button = document.getElementById("button");
            button.addEventListener("click", popup, false);

        });
    </script>

</polymer-element>

这行不通。我在 Firefox 中收到错误消息:“SyntaxError: missing : after property id. Chrome 反而说:“SyntaxError: Unexpected Identifier”。

都指向“var button = document.getelementById("button"); 行”

根据 Polymer 文档,javascript 应该放在文件末尾: https://www.polymer-project.org/docs/start/tutorial/step-1.html

所以在第二次尝试中,我将 Javascript 直接放在模板标签中,如下所示:

<polymer-element name="my-element">

    <template>
        <style>
        #content {background-color: #f2f2f2;}
        #button {padding: 10px 20px;}
        </style>

        <div id="content">
            <h1 id="title">Title</h1>

            <p>This example uses the addEventListener() method to attach a click event to the document.</p>


            <button id="button">Testbutton</button>
        </div>

        <script>
            function popup() {
                alert("jajaja");
            };

            var button = document.getElementById("button");
            button.addEventListener("click", popup, false);
        </script>

    </template>

    <script>
        Polymer('my-element', {


        });
    </script>

</polymer-element>

但是这一次我得到:“Uncaught TypeError: Cannot read property 'addEventListener' of null”,它指向我写的行:“button.addEventListener("click", popup, false);"。

我猜这意味着编译器看不到按钮 id,因为它在 Shadow-Dom 中?

请指教。

【问题讨论】:

  • 我不知道如何使用聚合物,但这显然是无效的 JS 语法。它从一个对象开始,然后在中间切换到代码......
  • 嘿 Karoly,你建议怎么写?也许这真的只是我对 javascript 的限制,这阻碍了我。我写了三遍代码。您指的是哪一点,语法是否无效,您将如何编写?
  • 您收到错误消息,告诉您您使用了无效的语法... ffs,请阅读错误消息。
  • 当我将代码实现到聚合物中时,我只会得到错误。在原始 html 文档中,我没有收到任何错误。我在上面写的语法错误,即使在谷歌搜索之后,我也找不到错误,这就是我在这里寻求帮助的原因。

标签: javascript polymer


【解决方案1】:
<polymer-element name="my-element">

  <template>
    <style>
    #content {background-color: #f2f2f2;}
    #button {padding: 10px 20px;}
    </style>

    <div id="content">
        <h1 id="title">Title</h1>

        <p>This example uses the addEventListener() method to attach a click event to the document.</p>


        <button on-tap="{{popup}}">Testbutton</button>
    </div>

  </template>

  <script>
    Polymer('my-element', {
        popup: function() {
            alert('alert');
        }
    });
  </script>

</polymer-element>

在聚合物元素内,您可以使用 on-tap="{{function}}" 属性直接绑定到函数。

已编辑:所有代码都没有进入块

在 js 方法中保留事件监听器

<polymer-element name="my-element">

  <template>
    <style>
    #content {background-color: #f2f2f2;}
    #button {padding: 10px 20px;}
    </style>

    <div id="content">
        <h1 id="title">Title</h1>

        <p>This example uses the addEventListener() method to attach a click event to the document.</p>


        <button id="button">Testbutton</button>
    </div>

  </template>

  <script>
    Polymer('my-element', {
        ready: function () {
            var button = this.$.button;
            button.addEventListener("click", function () {
                alert('alert');
            });
        }
    });
  </script>

</polymer-element>

再次编辑:OP 希望将 html 与 JS 分开

再编辑 1 次:如果不使用声明性方法,我认为使用匿名函数是最简单的方法。代码编辑

【讨论】:

  • 我希望将我的 html 与我的 javascript attirubtes 完全分开,例如 on-tap。我会测试这个解决方案,稍微研究一下,几分钟后回来。
  • 谢谢吉米。不想当老二,但我得到:“Uncaught ReferenceError: popup is not defined”。
  • 可能会改变函数的顺序。所以它在准备好之前定义了弹出窗口。生病编辑agian。如果这不起作用,您可以执行匿名函数 button.addEventListener('click', function () { alert('alert'); });
  • 谢谢 Jimi,打扰您了。你能不能最后解释一下,我可以在哪里读到“this.$.button”?我想使用这样的东西:“var button = document.querySelector("#button");"
  • this.$.button 是内部聚合物自定义元素的自动选择器。允许元素在 shadowdom 中轻松找到 id。 document.querySelector('#button') 是一个窗口级选择器,据我了解,它不能在自定义元素中使用。 (我可能错了,没有尝试过。IMO 输入更少的代码更容易)它没有麻烦。不问问题就很难学习。
【解决方案2】:

脚本标签位于结束模板标签和结束聚合物元素标签之间或元素定义之前,但在这种情况下,当您调用 Polymer('my-element') 时,您需要包含标签名称

这是上面带有外部 js 文件的示例,您可以使用 css 做同样的事情

<polymer-element name="my-element">

  <template>
   <link rel="stylesheet" href="my-element.css">

    <div id="content">
        <h1 id="title">Title</h1>

        <p>This example uses the addEventListener() method to attach a click event to the document.</p>


        <button id="button">Testbutton</button>
    </div>

  </template>
  <script src="my-element/my-element.js"></script>
</polymer-element>

文件夹名称和 js 文件名可以是任何你想要的,但我认为你应该像这样构建良好的做法

文件夹结构

-myelements -folder 
--my-first-element -folder
  --my-first-element.html
  --my-first-element.css
  --my-first-element.js
--my-second-element -folder
  --my-second-element.html
  --my-second-element.css
  --my-second-element.js

<polymer-element name="ouch-button">
  <template>
    <button on-click="{{onClick}}">Send hurt</button>
  </template>
  <script>
    Polymer({
      onClick: function() {
        alert('ouch', {msg: 'That hurt!'}); // fire(type, detail, targetNode, bubbles?, cancelable?)
      }
    });
  </script>
</polymer-element>

<ouch-button></ouch-button>

【讨论】:

  • 我已经删除了这些链接,因为它们在 Polymer 1.0 中已经过时了,尽管我相信上述包含外部脚本的方法仍然有效。
猜你喜欢
  • 2013-12-16
  • 2020-09-30
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
  • 2016-08-25
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多