【问题标题】:::content polyfill for VanillaJS Templates and Custom Elements::VanillaJS 模板和自定义元素的内容 polyfill
【发布时间】:2014-06-30 19:31:16
【问题描述】:

好像是聚合物的

polyfill-next-selector { content: ':host #myId' }

不适用于 VanillaJS <template>s 和自定义元素(在 IE 中)。

对于<polymer-element>,它工作正常:http://codepen.io/robdodson/pen/FokEw/,但由于某种原因,当我尝试对原生 JS 做同样的事情时,它却没有

<template>
  <style>
    polyfill-next-selector { content: ':host h1' }
    ::content h1 {
    color: red;
    }
  </style>
  ..ShadowDOM stuff..
  <content></content>
</template>

<my-element>
  <h1>Hello World, I'm red content of Custom Element</h1>
</my-element>

http://codepen.io/tomalec/pen/apqgr

shim-shadowdom 属性也没有帮助。

有什么解决方法,还是我用错了?

【问题讨论】:

    标签: javascript html polymer web-component shadow-dom


    【解决方案1】:

    匀场样式是 polymer.js(加糖)自动为您做的事情。它将样式表 -> &lt;style&gt; 内联到一个元素中,填充这些样式,并将它们添加到 polyfill 下的文档头部。

    如果您使用的是原版的东西,您必须手动进行匀场并手动添加。如果您在 &lt;style&gt; 或样式表中包含 shim-shadowdom 属性,这应该可以正常工作,但 polymer.js 和 platform.js 之间仍有一些重叠。

    解决方案!...in createdCallback():

    if (Platform.ShadowCSS) {
      var style = template.querySelector('style');
    
      var cssText = Platform.ShadowCSS.shimCssText(
          style.textContent, 'my-element');
      Platform.ShadowCSS.addCssToDocument(cssText);
    }
    

    演示:http://jsbin.com/xadocene/3/edit

    注意,我正在检查Platform.ShadowCSS,因为它在本机 Shadow DOM 下不存在,因此无需做额外的工作。

    请参阅http://www.polymer-project.org/docs/polymer/styling.html#manualshim 了解更多信息。

    【讨论】:

    • 在较新的版本中是否更改为WebComponents.ShadowCSS.shimStyling(template.content, 'my-element')? (discussion in this issue) 查询样式元素为我返回null,但模板上的shimStyling 似乎有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 2023-04-03
    • 2021-06-18
    相关资源
    最近更新 更多