普通JS onclick按钮最佳做法

时间:2019-04-10 13:07:23

标签: javascript html

我有2个按钮可以抛出一个普通的JS函数(我不想使用Jquery),但是我问这些方法之间的最佳实践是什么:

function next() {
  var elements = document.getElementById('choices-multiple-remove-button');
  var param = [];
  for (var i = 0; i < elements.length; i++) {
    param.push(elements[i].value);
  }
  console.log(param);
}

function hello() {
  var elements = document.getElementById('choices-multiple-remove-button');
  var param = [];
  for (var i = 0; i < elements.length; i++) {
    param.push(elements[i].value);
  }
  console.log(param);
}
document.getElementById("workingbutton").addEventListener("click", hello);
<button id="next" class="btn btn-info" onclick="next()" type="button">Next</button>
<button id="workingbutton" class="btn btn-info" type="button">Next</button>

0 个答案:

没有答案