Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
857 views
in Technique[技术] by (71.8m points)

dart - What are the different ways to look up elements in Polymer 1.0

There are several way in Polymer.dart 1.x to look up elements in the DOM. What are the differences.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  <body>
    <div id="top"></div>
    <app-element>
      <div id="child1"></div>
      <div id="child2"></div>
    </app-element>
    <script type="application/dart" src="index.dart"></script>
  </body>
<dom-module id='app-element'>
  <template>
    <button on-click="clickHandler">Query</button>
    <div id="shadow1"></div>
    <div id="shadow2"></div>
    <content></content>
  </template>
</dom-module>

The app_element.dart contains this import

import 'dart:html' as dom;

With shady DOM (default)

  • $["shadow1"] (works only for statically added elements)
    • shadow1
  • dom.querySelectorAll('div')
    • (a <div hidden> dynamically inserted by Polymer)
    • top
    • shadow1
    • shadow2
    • child1
    • child2
  • querySelectorAll('div')
    • shadow1
    • shadow2
    • child1
    • child2
  • Polymer.dom(this).querySelectorAll('div')
    • child1
    • child2
  • Polymer.dom(this.root).querySelectorAll('div')
    • shadow1
    • shadow2
  • $$('div') (returns the first element found by this selector)
    • shadow1

With shadow DOM (global setting to opt in)

  • $["shadow1"] (works only for statically added elements)
    • shadow1
  • dom.querySelectorAll('div')
    • (a <div hidden> dynamically inserted by Polymer)
    • top
    • child1
    • child2
  • querySelectorAll('div')
    • child1
    • child2
  • Polymer.dom(this).querySelectorAll('div')
    • child1
    • child2
  • Polymer.dom(this.root).querySelectorAll('div')
    • shadow1
    • shadow2
  • $$('div') (returns the first element found by this selector)
    • shadow1

Instead of .querySelectorAll(...) .querySelector(...) can be used of course but because it will always return one of the elements returned by .querySelectorAll(...) I didn't explicitely add these examples.

Enabling shadow DOM works the same in Polymer.dart 0.17 as explained here for Polymer.js


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...