[英]Access html inside content tag in Polymer
我正在尝试访问我在自己的聚合物元素中编写的html,但它无法正常工作。 我该如何实现?
index.html
<my-element> content I am trying to access </my-element>
my-element.html
<polymer-element name='my-element'>
<template>
<content id='content'></content>
</template>
<script>
Polymer({
ready: function(){
console.log('html: '+this.$.content.innerHTML);
}
});
</script>
</polymer-element>
安慰
html:
不幸的是,执行插入内容节点为兄弟姐妹 content
,而不是嵌套节点。 也就是说,一切准备就绪后,文档的结构将如下所示:
<content id='content'></content>
content I am trying to access
不像您预期的那样:
<!-- THAT IS NOT HOW IT’S COMPOSED -->
<content id='content'>content I am trying to access</content>
幸运的是,您仍然可以访问模板上下文中的childNodes
:
Polymer({
domReady: function() {
var content = '';
this.childNodes.array().forEach( function(el) {
content += el.textContent; // do whatever you want here
});
console.log(content);
}
});
//⇒ content I am trying to access
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.