简体   繁体   English

导入 HTML 后 eventListener 不起作用

[英]eventListener doesn't work after imported HTML

I got this app in progress:我正在开发这个应用程序:

https://jsfiddle.net/cg7Lwo29/2/ https://jsfiddle.net/cg7Lwo29/2/

So, I'm working with web pack and I'm trying to hold everything in different files.所以,我正在使用 web pack,我试图将所有内容保存在不同的文件中。 My HTML should be pretty basic for this ToDO app, so I'm trying to make just a few divs and change the content with JS.对于这个 ToDO 应用程序,我的 HTML 应该是非常基本的,所以我试图只制作几个 div 并使用 JS 更改内容。

So, I have this HTML code in a different js file and I import it in the index.js and then it goes to main.js所以,我在不同的 js 文件中有这个 HTML 代码,我将它导入 index.js 然后它转到 main.js

const createProject =
`
<div class="container">
<div class="container-header">ADD A PROJECT</div>

<form id='todo-form' action="get">
    <div class="todo-title"><span>Title</span><br><input id='title-input' type="text" name="" id=""></div>
    <div class="todo-description"><span>Description</span><br><input id='description-input' type="text" name="" id=""></div>
    <div class="todo-date"><span>Date</span><br><input id='date-input' type="date"></div>
    <div class="todo-priority"><span>Importance</span><br><input id='range-input' type="range" min='1' max='10' value='5' class='slider' id='myrange'></div>
    <div class="todo-button"><button id='submit-project'>Add project</button></div>
</form>  

</div>
`

export default createProject;

Everything good, I import it and it works HTML wise but JS doesn't recognise it I think.一切都很好,我导入它并且它可以在 HTML 中正常工作,但我认为 JS 无法识别它。

I get a null error at first eventListener我在第一个 eventListener 时收到空错误

        formSubmit.addEventListener('click', (e) =>{
            e.preventDefault();

But if I put the HTML directly, without importing it, it works just fine.但是如果我直接将 HTML 放入而不导入它,它就可以正常工作。 How can I solve this?我该如何解决这个问题?

TL:DR: HTML imported gives null error. TL:DR:导入的 HTML 出现空错误。

Cheers干杯

You're trying to add an eventListener when the HTML code for that particular id doesn't exist yet.当该特定 id 的 HTML 代码尚不存在时,您正尝试添加一个 eventListener。 That's why it returns as null.这就是它返回 null 的原因。 Make sure that you add the eventListener to the js file that contains the const createProject like this:确保将 eventListener 添加到包含 const createProject 的 js 文件中,如下所示:

const createProject =
`
<div class="container">
<div class="container-header">ADD A PROJECT</div>

<form id='todo-form' action="get">
    <div class="todo-title"><span>Title</span><br><input id='title-input' type="text" name="" id=""></div>
    <div class="todo-description"><span>Description</span><br><input id='description-input' type="text" name="" id=""></div>
    <div class="todo-date"><span>Date</span><br><input id='date-input' type="date"></div>
    <div class="todo-priority"><span>Importance</span><br><input id='range-input' type="range" min='1' max='10' value='5' class='slider' id='myrange'></div>
    <div class="todo-button"><button id='submit-project'>Add project</button></div>
</form>  

</div>
`

formSubmit.addEventListener('click', (e) =>{
            e.preventDefault();

export default createProject;

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

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