简体   繁体   English

定制聚合物元素 <x-strong> 像内置函数那样工作<strong>?</strong>

[英]Custom Polymer element <x-strong> that works like the builtin <strong>?

How do you create a custom element such as <x-strong> that works like the builtin <strong> ? 如何创建类似于内置<strong>的自定义元素(如<x-strong> <strong>

I've got as far as the following: 我有以下内容:

<polymer-element name="x-strong" noscript>
  <template>
    <style>
      x-strong {
        font-weight: bold;
      }
    </style>
    ???
  </template>
</polymer-element>

HTML: HTML:

<x-strong>Hello, <em>Clem</em></x-strong> 
// Would like this to render exactly the same as
<strong>Hello, <em>Clem</em></strong>

However, this has at least two problems: 但是,这至少有两个问题:

  1. I don't know how to get at the contents/children of the <x-strong> element. 我不知道如何获得<x-strong>元素的内容/子元素。 (All of the examples I've found show how to access attributes from the custom element, but not its content.) (我发现的所有示例都显示了如何访问自定义元素的属性,而不是其内容。)
  2. For some reason the CSS selector within the <style> element needs to be x-strong -- body , html and * all don't work. 由于某些原因, <style>元素内的CSS选择器必须是x-strong bodyhtml*均不起作用。

Adding/removing the lightdom and noscript attributes modify the behaviour in slightly different ways, but no combination seems to replicate the builtin element. 添加/删除lightdomnoscript属性会以稍微不同的方式修改行为,但是似乎没有任何组合可以复制内置元素。 Extending <strong> also doesn't work (although I actually want to do this from scratch, as an exercise). 扩展<strong>也不起作用(尽管我实际上想从头开始做一个练习)。

To render content from the light dom into your Polymer element's shadow use an insertion point : <content> . 要将内容从灯光dom渲染到Polymer元素的阴影中,请使用插入点<content> Also to style the host element, you can use the :host selector. 同样,要设置host元素的样式,可以使用:host选择器。 Both are features of Shadow DOM. 两者都是Shadow DOM的功能。

<polymer-element name="x-strong" noscript>
<template>
  <style>
    :host {
      font-weight: bold;
    }
  </style>
  <content></content>
</template>
</polymer-element>

Demo: http://jsbin.com/EqaxOTo/1/edit 演示: http//jsbin.com/EqaxOTo/1/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM