[removed] onclick/onsubmit for dynamically created button

前端 未结 2 1978
终归单人心
终归单人心 2020-12-14 23:21

I dynamically create a button in the way I found in the Internet:

 Page = function(...) {
   ...
 };

 Page.prototype = {
   ...
   addButton : function() {         


        
相关标签:
2条回答
  • 2020-12-14 23:29

    Here is a version with attributes for input:

     <button onclick="myFun()">Add More</button>
        <script>
            function myFun() {
                var x = document.createElement("INPUT");
                x.setAttribute("type", "file");
                x.setAttribute("id", "file");
                document.body.appendChild(x);
                x.onchange = function () {
                    hello();
                };
                btn.appendChild(t);
                document.body.appendChild(btn);
            }
            function hello() {
                window.alert("hello!");
            }
        </script>
    
    0 讨论(0)
  • 2020-12-14 23:33
    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);
    };
    
    0 讨论(0)
提交回复
热议问题