access DOM in polymer

眉间皱痕 提交于 2019-12-25 09:01:15

问题


What I want to achieve:

  1. highlight some text in p element

  2. (not add/ or remove) paper-badge when value is 0

  3. change paper-badge color depending on number

Sample module to play with:

<link rel="import" href="https://polygit2.appspot.com/components/polymer/polymer.html">
<link rel="import" href="https://polygit2.appspot.com/components/iron-icons/communication-icons.html">
<link rel="import" href="https://polygit2.appspot.com/components/paper-icon-button/paper-icon-button.html">
<link rel="import" href="https://polygit2.appspot.com/components/paper-badge/paper-badge.html">

<dom-module id="question-one">
    <template>
        <template is="dom-repeat" items="{{numbers}}">
            <p>color one of my letters</p>
            <paper-icon-button id="someid{{index}}" icon="communication:email"></paper-icon-button>
            <paper-badge for="someid{{index}}" label="{{item}}"> </paper-badge>
        </template>
    </template>
    <script>
        Polymer({
            is: "question-one",
            properties: {
                numbers: {
                    type: Array,
                    value: function () {
                        return [0, 1, 2, 0, 4, 5, 0, 7, 8, 0]
                    }
                }
            }
        }); </script>
</dom-module>

and usage:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://polygit2.appspot.com/components/webcomponentsjs/webcomponents-lite.js"></script>
    <link rel="import" href="questions//question-one.html" />
</head>
<body>
    <question-one></question-one>
</body>
</html>

回答1:


Add an id attribute

<p id="someId">

use the $ map to get a reference to the element

this.$.someId.innerText...

this.$ doesn't work for dynamically added HTML or HTML inside template is="dom-if" ortemplate is="dom-repeat" or similar.

For such cases you can use this.$$(selector) where all CSS selectors are supported, while this.$... only supports id (without #)



来源:https://stackoverflow.com/questions/37805587/access-dom-in-polymer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!