2015-06-02 130 views
3

我就迁移到聚合物1.0选择元件(聚合物1.0)

在这里工作是我的模板:

<template> 
    <div class="scroll"> 
     <div class="content"> 
      <content></content> 
     </div> 
     <div class="bar barY"></div> 
    </div> 
</template> 

内容被填充在主HTML文件中的文本。

我需要得到这个div的滚动高度。我曾经这样做:

height = $(this.shadowRoot).find('.content')[0].scrollHeight; 

但这不是工作了:

Uncaught TypeError: Cannot read property 'scrollHeight' of undefined 

我尝试添加一个id到div,并喜欢选择它这样:

height = this.$.content.scrollHeight; 

但尽管内容中有很多文字,但这给我的值为0。

我从ready函数调用此代码。

我是否正确选择元素?

回答

7

<content>实际上不包含该组件的内容,而它提供了一个插入点那些内容,这将是兄弟姐妹的<content>元件。要获得被插入一个给定<content>节点的元素,你可以使用以下命令:

var content = Polymer.dom(this.root).querySelector('content'); 
var distributed = Polymer.dom(content).getDistributedNodes() 

的文档上面可以有一个更完整的示例一起https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#dom-api-examples被发现。

+0

非常感谢,做到了。现在我在选择属性的语法上遇到了一些麻烦,但我会在另一个问题中提出这个问题。 – Tyler