简体   繁体   English

html按钮的动态onclick调用

[英]Dynamic onclick call of html buttons

In my javascript I have an array of 3 elements: 在我的JavaScript中,我有3个元素的数组:

var array = ["one", "two", "three"]; 

How do I create 3 html buttons such that each button's onclick calls a function with parameter array[i]. 如何创建3个html按钮,以便每个按钮的onclick调用带有参数array [i]的函数。

for result i want to have: 对于结果,我想拥有:

<button onclick='function("one")'></button>
<button onclick='function("two")'></button>
<button onclick='function("three")'></button>

To get you running quick here is a quick fix to your existing code. 为了使您快速运行,这里是对现有代码的快速修复。

If you say the buttons will be in order then you can make use of the indexes of the button. 如果您说按钮会按顺序排列,那么您可以利用按钮的索引。

 var array = ["one", "two", "three"]; function Myfunction(e){ var index = $('.container button').index(e.target) alert(array[index]); // now you have the value here, use it for your further stuff } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <button onclick='Myfunction(event)'></button> <button onclick='Myfunction(event)'></button> <button onclick='Myfunction(event)'></button> </div> 


If you are looking for a cleaner way then I would recommend not to use the onclick attribute to bind event and directly bind events using Jquery. 如果您正在寻找一种更清洁的方法,那么我建议您不要使用onclick属性来绑定事件,而不要使用Jquery直接绑定事件。 Like below.. 像下面

 var array = ["one", "two", "three"]; $('.container button').on('click', function() { var index = $('.container button').index($(this)); alert(array[index]); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <button ></button> <button ></button> <button ></button> </div> 

Using inline handler will give us troubles at times. 使用内联处理程序有时会给我们带来麻烦。 For example by accessing current element using this inside the event handler. 例如,通过在事件处理程序中使用this元素访问当前元素。 So here in this case, You can do it with dataset and addEventListener . 因此,在这种情况下,您可以使用datasetaddEventListener做到这一点。

var array = ["one", "two", "three"];
array.forEach(function(itm){
  var elem = document.createElement("button");
  elem.textContent = itm;
  elem.dataset.data = itm;
  elem.addEventListener("click", clickHandle, false);
  document.body.appendChild(elem);
});

function clickHandle(){
  alert(this.dataset.data); //will display "one"/"two"/"three" based on the element clicked.
}

DEMO 演示

var btn_list;   //The buttons parent element
var your_func;  //callback function
for (var i = 0; i < array.length; i++)
{
    btn = document.createElement('BUTTON'); //create element
    btn.onclick = function() //assign callback, with item
    {
        your_func(array[i]);
    };
    btn_list.appendChild(btn); //and add it to parent element
}

where of course, your callback function and your buttons parent element were defined before.... 当然,您在哪里定义了回调函数和按钮的父元素。

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

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