简体   繁体   English

我如何为每个按钮添加功能?

[英]how do i add functionality to each of the buttons?

The first part of the code is working correctly, but now that each button appears, how do i add functionality to each of them?代码的第一部分工作正常,但是现在每个按钮都出现了,我如何为每个按钮添加功能? currently the only button which does something when pressed is always the last one, the rest do nothing.目前唯一一个按下时执行某事的按钮总是最后一个,其余的什么都不做。

Change it to将其更改为

{

                var output = ""; 
                var data = JSON.parse(e.target.responseText);
                   for(var i=0; i<data.length; i++) 
                {
                    output  = data[i].title + ' ';
                    var p = document.createElement("p");
                    var div = document.getElementById("response");
                    var textNode = document.createTextNode(output);
                    p.appendChild(textNode);
                    var button = document.createElement("button");
                        button.innerHTML = "Download";
                        p.appendChild(button);

                    div.appendChild(p);
                    button.addEventListener ("click", () => 
                    {
                        alert("Test");
                    });
                }
}

You are adding the below code out side the for loop您正在 for 循环之外添加以下代码

button.addEventListener ("click", () => 
 { 
    alert("Test");
 } );

Keep the above code inside the for loop.将上述代码保留在 for 循环中。 So that for each button the event listener will be added.这样对于每个按钮,都会添加事件侦听器。

Another way to approach this would be to add the callback function to the onclick variable of the elements prototype:解决此问题的另一种方法是将回调函数添加到元素原型的onclick变量中:

 function doStuff() {
   var output = "";
   var data = JSON.parse(e.target.responseText);
   for (var i = 0; i < data.length; i++) {
     output = data[i].title + ' ';
     var p = document.createElement("p");
     var div = document.getElementById("response");
     var textNode = document.createTextNode(output);
     p.appendChild(textNode);
     var button = document.createElement("button");
     button.innerHTML = "Download";

     // Adds the callback function here
     button.onclick = () => {
       // fill in your arrow function here...
         alert("Test");
     };
     p.appendChild(button);
     div.appendChild(p);
   };
 }

doStuff();

Here is a jsFiddle是一个jsFiddle

You should use event delegation for dynamically added elements您应该对动态添加的元素使用事件委托

 // sample data var data = [{ title: 'one' }, { title: 'two' },{ title: 'three' }]; var output = ""; for (var i = 0; i < data.length; i++) { var output = data[i].title + " "; var p = document.createElement("p"); var div = document.getElementById("response"); var textNode = document.createTextNode(output); p.appendChild(textNode); var button = document.createElement("button"); // added output to button text for identification button.innerHTML = output + " Download"; p.appendChild(button); div.appendChild(p); } // Get the parent element, add a click listener document.getElementById("response").addEventListener("click", function(e) { // e.target is the clicked element! // If it was a button if (e.target && e.target.nodeName == "BUTTON") { // Button found! Output the identifying data! // do other work on click document.getElementById("display").innerHTML = e.target.innerHTML + " Clicked"; } });
 <div id="response"></div> <div id="display">Display</div>

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

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