[英]Why do I get null when accessing firstElementChild here?
I am following a tutorial where I have the following HTML我正在关注一个教程,其中我有以下 HTML
<template id="project-input">
<form>
<div class="form-control">
<label for="title">Title</label>
<input type="text" id="title" />
</div>
<div class="form-control">
<label for="description">Description</label>
<textarea id="description" rows="3"></textarea>
</div>
<div class="form-control">
<label for="people">People</label>
<input type="number" id="people" step="1" min="0" max="10" />
</div>
<button type="submit">ADD PROJECT</button>
</form>
</template>
I want to take the form
in this template and render it inside:我想采用此模板中的
form
并将其呈现在内部:
<div id="app"></div>
In my code I have the following class:在我的代码中,我有以下 class:
class ProjectInput {
templateElm: HTMLTemplateElement;
appElm: HTMLDivElement;
element: HTMLFormElement;
constructor() {
//I start by getting a reference to the template
this.templateElm = document.getElementById('project-input') as HTMLTemplateElement;
//And a reference to the div
this.appElm = document.getElementById('app') as HTMLDivElement;
//I import the contents of the template
const importedNode = document.importNode(this.templateElm, true);
//And access the form which is the first child
//This assignment becomes null for some reason
this.element = importedNode.firstElementChild as HTMLFormElement;
//I call the attach method which should render the form inside the div
//This of course results in an error since this.element is null
this.attach();
}
attach() {
this.appElm.insertAdjacentElement('afterbegin', this.element);
}
}
const p = new ProjectInput();
For some reason the assignment of this.element = importedNode.firstElementChild as HTMLFormElement;
由于某种原因
this.element = importedNode.firstElementChild as HTMLFormElement;
becomes null
.变为
null
。
Can anyone figure out why?谁能弄清楚为什么?
I think the problem here is that you need to use another function istead of importNode()
.我认为这里的问题是您需要使用另一个 function 而
importNode()
。 I am not able to post comments, so here is my answer.我无法发表评论,所以这是我的答案。
importedNode
is null.importedNode
是否为null。insertAdjacentElement
, because the official MDN saysinsertAdjacentElement
,因为官方 MDN 说The imported node is not yet included in the document tree.
导入的节点尚未包含在文档树中。 To include it, you need to call an insertion method such as appendChild() or insertBefore() with a node that is currently in the document tree.
要包含它,您需要使用当前位于文档树中的节点调用插入方法,例如 appendChild() 或 insertBefore()。
firstElementChild
is not working with Nodes, that are not appened.firstElementChild
不适用于未附加的节点。 Maybe try importedNode.querySelector('form')
instead of getting first element.importedNode.querySelector('form')
而不是获取第一个元素。 I think that the main issue here is because you put the form in a template and it's not an HTMLElement, it's actually an HTMLTemplateElement我认为这里的主要问题是因为您将表单放在模板中并且它不是 HTMLElement,它实际上是 HTMLTemplateElement
https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement
And there for you need the inside content (the final form html element), and insert it to document with append or prepend您需要内部内容(最终形式 html 元素),并将其插入到带有append的文档中或预先添加
So I would suggest to change little bit the code to所以我建议将代码更改为
class ProjectInput {
constructor() {
this.templateElm = document.getElementById('project-input');
this.appElm = document.getElementById('app');
const importedNode = document.importNode(this.templateElm, true);
this.element = importedNode.content;
this.attach();
}
attach() {
this.appElm.prepend(this.element);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.