简体   繁体   English

动态创建的元素中的Click事件

[英]Click event in dynamically created Element

I am new to Javascript and learning new things day by day. 我不熟悉Javascript,并且每天都在学习新事物。 Here I need to click the button and new button are created now I need to again click that newly created button and create new button again and so on. 在这里,我需要单击按钮,然后创建新按钮,现在我需要再次单击该新创建的按钮,然后再次创建新按钮,依此类推。 It must be in Pure Javascript.Please help me out 它必须使用纯Javascript。请帮帮我

 document.getElementById("btn").addEventListener("click",function(e) {
        var btn=document.createElement("BUTTON");
        var t=document.createTextNode("Click Me");
        //Some code to click dynamically created element
        btn.appendChild(t);
        document.body.appendChild(btn);

Create a function to create a button that creates a button. 创建一个函数来创建一个创建按钮的按钮。 Note: you're not appending the text node to the button. 注意:您没有将文本节点附加到按钮。

If you want to watch changes in the DOM and add events to the buttons in a alternative way, check the answers in this question ... 如果您想观看DOM中的更改并将事件添加到按钮中,请查看此问题的答案...

var body = document.getElementsByTagName('body')[0];

(function myButton() {

    var btn = document.createElement("BUTTON");
    var text = document.createTextNode("Click Me");

    // append the text to the button
    btn.appendChild(text);

    // append the btn to the body tag
    body.appendChild(btn);

    // adds the click event to the btn
    btn.addEventListener("click", myButton);

})();

Make it a generic function and bind the click events to that method. 使它成为通用函数,并将click事件绑定到该方法。

 function addButton () { var btn = document.createElement("BUTTON"); btn.type = "button"; var t = document.createTextNode("Click Me"); btn.appendChild(t); btn.addEventListener("click", addButton); document.body.appendChild(btn); } document.getElementById("btn").addEventListener("click", addButton); 
 <button type="button" id="btn">Button</button> 

Or event delegation 或事件委派

 function addButton () { var btn = document.createElement("BUTTON"); btn.type = "button"; var t = document.createTextNode("Click Me"); btn.appendChild(t); document.body.appendChild(btn); } document.body.addEventListener("click", function(e) { if (e.target.tagName==="BUTTON") { //I personally would use class or data attribute instead of tagName addButton(); } }); 
 <button type="button" id="btn">Button</button> 

In this case, jQuery do the good jobs for you. 在这种情况下,jQuery为您做好了工作。

$(function($){
  $(document).on('click','button',function(e){
    // do your stuff.
  })
})

Here is another good answer using jQuery: 这是使用jQuery的另一个很好的答案:

Event binding on dynamically created elements? 在动态创建的元素上进行事件绑定?

Simply add a eventlistener to document, then check tag. 只需在文档中添加一个事件监听器,然后检查标记即可。 You can further expland it by also adding a Id or Class to the buttons and check that aswell (in case you need multiple buttons that does different things) 您还可以通过在按钮上添加一个Id或Class来进一步扩展它,并进行检查(以防您需要多个执行不同功能的按钮)

 document.addEventListener('click', function(event) { var clickedEl = event.target; if(clickedEl.tagName === 'BUTTON') { clickedEl.innerHTML = "clicked!"; var btn=document.createElement("BUTTON"); var t=document.createTextNode("Click Me"); btn.appendChild(t); document.body.appendChild(btn); } }); 
 <button>Click Me</button> 

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

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