且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript:动态创建按钮的 onclick/onsubmit

更新时间:2023-11-23 09:33:58

var foo = function(){
  var button = document.createElement('button');
  button.innerHTML = 'click me';
  button.onclick = function(){
    alert('here be dragons');return false;
  };
  // where do we want to have the button to appear?
  // you can append it to another element just by doing something like
  // document.getElementById('foobutton').appendChild(button);
  document.body.appendChild(button);
};