我正在尝试使用以下代码将 iframe 的内容动态设置为 polymer 元素:
$(this.$.frame).contents().find('html').html("<custom-element></custom-element>");
不幸的是,这不起作用。 (iframe 只是空的)
有趣的是,当您不使用 polymer 元素时,上面的代码仍然有效。
例如
$(this.$.frame).contents().find('html').html("This works");
完美运行。
感谢您的帮助。
最佳答案
我怀疑问题出在 iframe 运行时有它自己的文档上下文和它自己的 javascript 上下文。这意味着您在文档中注册的自定义元素对于 iframe 是未知的。
尝试:
var source = '<script src='path/to/platform/platform.js'></script>';
source += '<link rel='import' href='path/to/custom-element/custom-element.html'>';
source += '<custom-element></custom-element>';
$(this.$.frame).contents().find('html').html(source);
关于javascript - 将 iframe 的内容设置为 polymer 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25487247/