简体   繁体   English

使用 vanilla JS 向动态创建的 html 元素添加点击事件

[英]Adding a click event to a dynamically created html element using vanilla JS

I want to create a simple div element in html with vanilla JS.我想用 vanilla JS 在 html 中创建一个简单的 div 元素。 I am trying to add EventListener to a button to create a div on click , but strange situation happening as i can't figure it out why?我正在尝试将 EventListener 添加到按钮以在单击时创建一个 div,但是发生了奇怪的情况,因为我不知道为什么?

I am trying to create a div under that [div element with container-div class], when i will click the button.当我单击按钮时,我正在尝试在该 [div 元素与容器 div 类] 下创建一个 div。 But when i clicking it function createDiv can't tale argument for height parameter.但是当我点击它时,函数 createDiv 不能为 height 参数提供参数。 why it's happening?为什么会这样? Help me please , much appreciated.请帮助我,非常感谢。

This pic for directly calling function before clicking button此图用于在单击按钮之前直接调用函数

In this pic red bordered area is growing after i clicked button several times在这张图片中,我多次点击按钮后,红色边框区域正在增长

I am trying to create a div without fill but ....我正在尝试创建一个没有填充的 div 但是....

 function createDiv( height = "200px", width = "200px", border = "solid 1px red" ) { const div = document.createElement("div"); div.style.height = height; div.style.width = width; div.style.border = border; document.getElementsByClassName("container-div")[0].appendChild(div); return div; } const createDivbtn = document.getElementById("createDiv"); createDiv(); createDiv(undefined, undefined, "solid 5px blue"); createDivbtn.addEventListener("click", createDiv);
 <div class="row"> <div class="col-6"> <button id="createDiv" class="btn btn-outline-black1"> Create Div </button> </div> <div class="col-6 container-div"></div> </div>

You need to add the click to the button您需要将点击添加到按钮

Also you return the div so you can use it for other stuff - see how here:此外,您还可以返回 div,以便您可以将其用于其他内容 - 请参阅此处:

I would move the insertion to after the creation我会将插入移动到创建之后

 function createDiv( height = "200px", width = "200px", border = "solid 1px red" ) { const div = document.createElement("div"); div.style.height = height; div.style.width = width; div.style.border = border; return div; } document.getElementById("createDiv").addEventListener("click",function() { const createdDiv1 = createDiv(undefined, undefined, "solid 5px blue"); createdDiv1.textContent = "1. created"; document.getElementsByClassName("container-div")[0].appendChild(createdDiv1); const createdDiv2= createDiv(undefined, "300px", "solid 5px green"); createdDiv2textContent = "2 created"; document.getElementsByClassName("container-div")[0].appendChild(createdDiv2); })
 <div class="row"> <div class="col-6"> <button id="createDiv" class="btn btn-outline-black1"> Create Div </button> </div> <div class="col-6 container-div"></div> </div>

When invoking a function by a DOM event, if no argument is provided, the first argument will be the evevnt itself.当通过 DOM 事件调用函数时,如果没有提供参数,第一个参数将是事件本身。

This is why in your case, by clicking the button, you get a div with no height.这就是为什么在您的情况下,通过单击按钮,您会得到一个没有高度的 div。 Because the height parameter value isn't '200px' but the event itself.因为高度参数值不是'200px'而是事件本身。 Try logging the height to the console and you will see it.尝试将高度记录到控制台,您将看到它。 mplungjan solve's the problem nicely in his answer. mplungjan 在他的回答中很好地解决了这个问题。

Good luck!祝你好运!

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

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