加载中...

1.2.7 找节点


weex中,可以通过在特定的节点上设置 id 属性,以此来唯一标识该节点。然后可以用 this.$el(id)来找到该节点,以scrollToElement() 为例,如下:

  1. <template>
  2. <container>
  3. <text id="top">Top</text>
  4. <container style="height: 10000; background-color: #999999;">
  5. </container>
  6. <text onclick="back2Top">Back to Top</text>
  7. </container>
  8. </template>
  9. <script>
  10. var dom = require('@weex-module/dom');
  11. module.exports = {
  12. methods: {
  13. back2Top: function () {
  14. var top = this.$el('top')
  15. dom.scrollToElement(top, { offset: 10 })
  16. }
  17. }
  18. }
  19. </script>

id 也可以在 repeat语法中使用,更多详见 展示逻辑控制,但是要确保循环的节点需要用不同的id,如下:

  1. <template>
  2. <container>
  3. <image id="{{imgId}}" src="{{imgUrl}}" onclick="getImageId" repeat="{{images}}"></image>
  4. </container>
  5. </template>
  6. <script>
  7. module.exports = {
  8. data: {
  9. images: [
  10. {imgId: 1, imgUrl: '...'},
  11. {imgId: 2, imgUrl: '...'},
  12. {imgId: 3, imgUrl: '...'},
  13. ...
  14. ]
  15. },
  16. methods: {
  17. getImageId: function(e) {
  18. // get e.target.id
  19. }
  20. }
  21. }
  22. </script>

另外,我们可以通过this.$vm(id) 方法可以访问子组件的上下文,用法见 组件封装。


第 1-1 条, 共 1 条.
童话 2017-07-03 13:48:29
var dom = require('@weex-module/dom'); 我是一步步跟着进来的,这里竟然没有讲这步的作用?
dom.scrollToElement又是什么鬼?虽然能猜出个大概,但是也需要讲解一下吧,差评回复