简体   繁体   English

如何在模板元素中使用 .innerHTML

[英]How to use .innerHTML in template element

I'm making an app with a framework that uses templates in HTML pretty heavily.我正在制作一个带有框架的应用程序,该框架大量使用 HTML 中的模板。 It was all going well until I bumped into this problem:一切都很顺利,直到我遇到了这个问题:

Uncaught TypeError: Cannot read property 'querySelector' of null

correct me if I'm wrong, but I think this has to do with my code itself using templates and JavaScipt not being able to read the inside of that template.如果我错了,请纠正我,但我认为这与我使用模板的代码本身有关,而 JavaScipt 无法读取该模板的内部。

Here's a minature version of my code which I am experiencing errors with.这是我的代码的一个缩影版本,我遇到了错误。

HTML HTML

<template id="main.html">
     <p id="paragraph">This text needs to be changed in 
     JS</p>
     <button onclick=example()>Click ME!</button>
</template>

JavaScript JavaScript

 function example (){
 document.getElementById("paragraph").innerHTML = "This text has been changed!"}

Error:错误:

 Uncaught TypeError: Cannot read property 'innerHTML' 
 of null

Is there any way of avoiding this error?有没有办法避免这个错误?

1) Template-element content is not rendered by default 1) 默认不渲染模板元素内容

You can re-use the content in a template-element as many times as you want, but you need to manually specify that using Javascript.您可以根据需要多次重复使用模板元素中的内容,但您需要使用 Javascript 手动指定。 If you don't use any Javascript to render your template-element it won't be visible in the DOM.如果您不使用任何 Javascript 来呈现您的模板元素,它将在 DOM 中不可见。 Resulting in your code not finding the paragraph (which is in the template-element).导致您的代码找不到段落(位于模板元素中)。

2) Make sure you call your Javascript after the DOM is loaded 2) 确保在加载 DOM 后调用 Javascript

A web-page gets loaded from top to bottom.网页从上到下加载。 If you don't place your Javascript-code on the bottom of your body tag, or manually say it needs to be loaded after the DOM is ready, your code tries to find an element before that element even exists.如果你没有将你的 Javascript 代码放在你的 body 标签的底部,或者手动说它需要在 DOM 准备好后加载,你的代码会尝试在该元素存在之前找到该元素。

3) Your code has small mistakes like missing paragraphs etc. 3)你的代码有一些小错误,比如缺少段落等。

Fix that using a good editor.使用一个好的编辑器解决这个问题。

This example code shows you how it can be done:此示例代码向您展示了如何完成:

Javascript: Javascript:

function example() {
    document.getElementById("paragraph").innerHTML = "This text has been changed!";
}


function init() {
    //Get the template-element by id
    let mainTemplate = document.getElementById("main");
    //Create a new div-element to hold template content
    let parent = document.createElement("div");

    //IMPORTANT!
    //Cloning template content into HTML DOM so it's visible.
    //This part - content.cloneNode - makes your template content visible after being appended to the body, or another legal element in the DOM.
    parent.append(mainTemplate.content.cloneNode(true));

    //Append to body
    document.body.append(parent);
}   

//After dom is ready: Launch init function
document.onload = init();

HTML: HTML:

<template id="main">
     <p id="paragraph">This text needs to be changed in 
     JS</p>
     <button onclick="example()">Click ME!</button>
</template>

Working JSFiddle: https://jsfiddle.net/ovc1mrs2/工作 JSFiddle: https ://jsfiddle.net/ovc1mrs2/

You probably want to append the content of your template directly to the body, or to another element.您可能希望将模板的内容直接附加到正文或其他元素。

I'm thinking you just forgot to put "()" after "function example."我想你只是忘了在“函数示例”之后加上“()”。

 function example() { document.getElementById("paragraph").innerHTML = "This text has been changed!"}
 <p id="paragraph">This text needs to be changed in JS</p> <button onclick=example()>Click ME!</button>

add "()" after example in js.在 js 中的示例后添加“()”。

Updated Javascript code :更新的 Javascript 代码:

function example () { document.getElementById("paragraph").innerHTML = "This text has been changed!" };

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

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