简体   繁体   English

从动态按钮 Javascript 进行测试

[英]Getting test from a dynamic button Javascript

I'm looking for a method to get a text from a button, which was dynamically created.我正在寻找一种从动态创建的按钮中获取文本的方法。 I've tried assigning an onclick when it's created, which only works for the last button.我尝试在创建 onclick 时分配它,它仅适用于最后一个按钮。

My code:我的代码:

for (var i = 0; i < 8; i++) {
  var divnode = document.createElement("div");
  document.getElementById("container").appendChild(divnode);

  var buttonnode = document.createElement("button");
  var textnode = document.createTextNode("Some Random Text" + i);
  buttonnode.appendChild(textnode);
  buttonnode.onclick = function() {
    console.log(buttonnode.innerHTML);
  }
  document.getElementById("container").childNodes[i + 1].appendChild(buttonnode);
}

Fiddle I've been working with: https://jsfiddle.net/tpwkzfor/我一直在使用的小提琴: https://jsfiddle.net/tpwkzfor/

The reason this is happening is because you are defining the function every iteration of the loop.发生这种情况的原因是因为您在循环的每次迭代中都定义了 function。 The way to fix this would be to pull the function definition outside of the loop, like this:解决此问题的方法是将 function 定义拉到循环之外,如下所示:

function printButton(event) {console.log(event.target.innerHTML)}

for (var i = 0; i < 8; i++) {
  var divnode = document.createElement("div");
  document.getElementById("container").appendChild(divnode);

  var buttonnode = document.createElement("button");
  var textnode = document.createTextNode("Some Random Text" + i);
  buttonnode.appendChild(textnode);
    buttonnode.onclick = printButton
  document.getElementById("container").childNodes[i + 1].appendChild(buttonnode);
}

Well the fix is pretty simple really.那么修复真的很简单。
Change your console.log(buttonnode.innerHTML) to console.log(this.innerHTML)将您的console.log(buttonnode.innerHTML)更改为console.log(this.innerHTML)

I found this to work for future reference.:我发现这可以供将来参考。:

for (var i = 0; i < 8; i++) {
    var divnode = document.createElement("div");
  document.getElementById("container").appendChild(divnode);

  var buttonnode = document.createElement("button");
  var textnode = document.createTextNode("Some Random Text" + i);
  buttonnode.appendChild(textnode);
  buttonnode.id = "btnSearch" + i;
    buttonnode.addEventListener("click", function(e) {
    if(e.target && e.target.nodeName == "BUTTON") {
    console.log(document.getElementById(e.target.id).innerText);
    }});
  document.getElementById("container").childNodes[i + 1].appendChild(buttonnode);
}

Try to access the innerHTML as e.target.innerHTML尝试以 e.target.innerHTML 的形式访问 innerHTML

Refer below code参考下面的代码

 for (var i = 0; i < 8; i++) { var divnode = document.createElement("div"); document.getElementById("container").appendChild(divnode); var buttonnode = document.createElement("button"); var textnode = document.createTextNode("Some Random Text" + i); buttonnode.appendChild(textnode); //pass event as argument buttonnode.onclick = function(e){ //console e.target.innerHTML console.log(e.target.innerHTML);} document.getElementById("container").childNodes[i + 1].appendChild(buttonnode); }
 <div> <label>Test</label> <div id="container"> </div> </div>

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

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