2015-06-02 50 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