admin管理员组文章数量:1387339
Edit: Just found this link, that explains it somehow. Using querySelector to find nested elements inside a Polymer template returns null
Nonetheless I'd appreciate an answer to my question on this specific code.
/Edit
I think a rather straight forward and minimalistic problem:
I've got a custom element in polymer:
<polymer-element name="my-test">
<template>
<div id="content">
<h3 id="test">My Test</h3>
<p id="paragraph">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi dolore consectetur, mollitia eos molestias totam ea nobis voluptatibus sed placeat, sapiente, omnis repellat dolores! Nihil nostrum blanditiis quasi beatae laborum quisquam ipsum!</p>
<button id="button">Clicke Me!</button>
</div>
</template>
<script>
Polymer('my-test', {
ready: function() {
var button = this.$.button;
button.addEventListener("click", function() {
alert("alert");
});
}
});
</script>
</polymer-element>
The thing is, that I would rather use
var button = document.querySelector(#button);
instead of
var button = this.$.button;
Because it seems more intuitive and declarative. But whenever I do this I get an error that says:
"Uncaught TypeError: Cannot read property 'addEventListener' of null " in Chrome and
Firefox instead says: "TypeError: button is null"
So any help is appreciated! :)
Edit: Just found this link, that explains it somehow. Using querySelector to find nested elements inside a Polymer template returns null
Nonetheless I'd appreciate an answer to my question on this specific code.
/Edit
I think a rather straight forward and minimalistic problem:
I've got a custom element in polymer:
<polymer-element name="my-test">
<template>
<div id="content">
<h3 id="test">My Test</h3>
<p id="paragraph">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi dolore consectetur, mollitia eos molestias totam ea nobis voluptatibus sed placeat, sapiente, omnis repellat dolores! Nihil nostrum blanditiis quasi beatae laborum quisquam ipsum!</p>
<button id="button">Clicke Me!</button>
</div>
</template>
<script>
Polymer('my-test', {
ready: function() {
var button = this.$.button;
button.addEventListener("click", function() {
alert("alert");
});
}
});
</script>
</polymer-element>
The thing is, that I would rather use
var button = document.querySelector(#button);
instead of
var button = this.$.button;
Because it seems more intuitive and declarative. But whenever I do this I get an error that says:
"Uncaught TypeError: Cannot read property 'addEventListener' of null " in Chrome and
Firefox instead says: "TypeError: button is null"
So any help is appreciated! :)
Share Improve this question edited Nov 8, 2014 at 22:34 Günter Zöchbauer 659k234 gold badges2.1k silver badges1.6k bronze badges asked Nov 8, 2014 at 21:50 LoveAndHappinessLoveAndHappiness 10.2k22 gold badges74 silver badges107 bronze badges2 Answers
Reset to default 6Your button is inside your Polymer element and because Polymer elements have their content inside their shadow DOM the element can't be found this way. You can either use this.shadowRoot.querySelector('#button');
or document.querySelector('* /deep/ #button');
or document.querySelector('my-test::shadow #button');
if your <my-test>
element is not within the shadow DOM of another element. /deep/
pierces through all shadow DOM boundaries ::shadow
just through one.
Actually following line:
var button = document.querySelector(#button);
Should be:
var button = document.querySelector('#button');
Notice the quotes.
本文标签: javascriptPolymer Using querySelector in Custom Element instead of quotthisquotStack Overflow
版权声明:本文标题:javascript - Polymer: Using querySelector in Custom Element instead of "this.$." - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744517057a2610227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论