如何从导入的html中的脚本访问导入的html的元素

米哈伊尔·博亚尔斯基(Mikhail Boyarsky)

我想制作一个自定义元素,它将在html导入时自动注册。

custom-element.html:

<html>
  <...>
  <body>
    <template id="myTemplate">
    </template>
  </body>
  <script src="my-script.js">
</html>

my-script.js:

let customElementPrototype = Object.create(HTMLElement.prototype);
customElementPrototype.createdCallback = function () {
  // somewhere here i want to access <template> to insert it in element
  // I've tried document.querySelector("#myTemplate") and
  // document.currentScript.ownerDocument.querySelector.querySelector("#myTemplate")
  // they don't work either, result of querySelector is null
}
document.registerElement("custom-element", customElementPrototype);

用法:

<html>
<head>
  <link rel="import" href="custom-element.html">
</head>
<...>
费利斯

createdCallback上下文内部custom-element而不是。ownerDocument.您可以console.log(this);createdCallbackwhich日志中看到此内容(来自我的plunker示例):

<div is="custom-element">
</div>

在中,my-script.js您可以捕获对的引用,ownerDocument并在回调中使用它。这将允许您的自定义元素访问其自身的DOM和导入的HTML的DOM。例如

var mainDoc = document.currentScript.ownerDocument;

完整my-script.jsscript.js在我的朋克中):

var mainDoc = document.currentScript.ownerDocument;

var CustomElementPrototype = Object.create(HTMLElement.prototype);
CustomElementPrototype.createdCallback = function () {
  console.log(this); //the div 
  //import the contents of the template
  var clone = document.importNode(mainDoc.querySelector('#myTemplate').content, true);
  //append template to div
  this.appendChild(clone);
};

document.registerElement("custom-element", {
    prototype: CustomElementPrototype,
    extends: 'div'
});

结合具有默认值的模板(此文档可通过访问mainDoc):

<html>
  <body>
    <script src="script.js"></script>
    <template id="myTemplate">
      hello world
      <br>
      from template
    </template>
  </body>
</html>

并将其全部使用的HTML:

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <link rel="import" href="custom-element.html">
  </head>

  <body>
    <h1>Hello Plunker!</h1>
    <div is="custom-element"></div>
  </body>

</html>

https://plnkr.co/edit/jO3ci8vLxolHhvtGxJQ5

这是一个有用的参考:http : //webcomponents.org/articles/introduction-to-html-imports/

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何导入HTML文件

来自分类Dev

如何导入HTML并插入到div中?

来自分类Dev

如何在 Firefox 中从 HTML 导入书签?

来自分类Dev

HTML模板和HTML导入-无法在Firefox中执行内部脚本

来自分类Dev

在core.cljs Clojure脚本中访问HTML Input元素

来自分类Dev

将 HTML 导入 HTML

来自分类Dev

如何在Python开发中解决“未解决的导入:HTML”?

来自分类Dev

如何使用 html 从 python 中的 web 导入表?

来自分类Dev

如何在javascript中导入在html中引用的这个库?

来自分类Dev

Gradle:如何从导入的脚本中调用“ def”?

来自分类Dev

如何导入脚本?

来自分类Dev

如何关注导入的React组件中的元素?

来自分类Dev

如何关注导入的React组件中的元素?

来自分类Dev

如何使用HTML正确导入img?

来自分类Dev

如何将HTML / JS导入Drupal?

来自分类Dev

HTML导入在Chrome中不起作用

来自分类Dev

HTML导入Internet Explorer中的加载顺序

来自分类Dev

无法导入html中的js文件

来自分类Dev

无法导入html中的js文件

来自分类Dev

从服务器导入的数据无法使用/访问html文件中嵌入的javascript函数

来自分类Dev

如何访问 html 元素的属性?

来自分类Dev

使用html或客户端脚本从另一个文本文件导入<head>元素

来自分类Dev

CSS未导入HTML

来自分类Dev

HTML导入的依赖注入

来自分类Dev

将XML导入HTML

来自分类Dev

Django导入csv HTML

来自分类Dev

拦截HTML导入

来自分类Dev

导入html的外部部分

来自分类Dev

拦截HTML导入