2014-09-20 27 views
2

爲什麼this.querySelector("#hello")找不到我的元素,this.$.hello確實找到它了?

+0

您可能需要提供更多的代碼......比如這是什麼?..它是文檔的一個實例嗎?通常,如果我可以確定元素是生成的查詢元素的父元素,那麼我使用document.querySelector或element.querySelector。 – 2014-09-20 02:46:14

+0

答案是this.shadowRoot.querySelector(「#hello」)在irc上得到它:)請求這個人在這裏回答:) – 2014-09-20 02:48:49

+0

但它似乎是你的問題更多的是這是什麼。$。hello爲什麼它返回結果...我不熟悉的用法,但它似乎是某種排名的全球參考 – 2014-09-20 02:49:05

回答

4

您正在尋找的元素最有可能位於元素的Shadow DOM中。儘量

this.shadowRoot.querySelector('#hello'); 
3
this.shadowRoot.querySelector('#hello') 

不推薦的方式,更好的辦法是使用內置聚合物功能:

this.$$('#hello') 

爲什麼?

  1. 這是很多短
  2. 它運行在高分子功能Polymer.dom(this.root).querySelector(selector),這是使用優化的聚合物

以及物品是否完整構建,你可能只是如果你可以使用this.$["hello"]不使用this.$.hello或只是不想

相關問題