小编Jus*_*kic的帖子

使用Shadow DOM扩充SVG​​标记

我一直在尝试HTML5和SVG; 我对JavaScript和Web开发都很陌生,所以我可能会缺少一些东西.我正在尝试创建可重用的Web组件,利用一些新功能,如HTML Imports,Shadow DOM和扩展现有的Web元素.我有两个html文件:

的test.html

<html>
<head>
  <title>Test HTML5</title>
</head>
<body>
  <link rel="import" href="Elements/tank.html">

  <svg width="100%"
       viewBox="0 0 500 500"
       style="border: solid; border-width: thin; stroke-width: 2;">

    <polyline points="0,300 100,300 100,100 250,100 250,150 500,150"
              style="fill:none;stroke:black;stroke-width:3"
              onclick="window.alert('you clicked line')" />
    <svg is="my-tank" x="200" y="350" width="50" height="50"></svg>

  </svg>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

tank.html

<html>
<body>
  <template id="tank">
    <rect fill="red" width="50" height="50"></rect>
  </template>

  <script type="text/javascript">
    var tankElement = document.currentScript.ownerDocument.querySelector('#tank');

    document.registerElement('my-tank', {
      prototype: Object.create(SVGSVGElement.prototype, {
        createdCallback: {
          value: function () {
            var root = this.createShadowRoot();

            //Works
            root.innerHTML …
Run Code Online (Sandbox Code Playgroud)

javascript html5 svg web-component shadow-dom

7
推荐指数
1
解决办法
2202
查看次数

标签 统计

html5 ×1

javascript ×1

shadow-dom ×1

svg ×1

web-component ×1