Add onclick property to input with JavaScript

前端 未结 4 1114
执笔经年
执笔经年 2020-12-17 09:41

I am creating a user input at one of the events:

var throwConnectBox = function() {
    chat_box = document.getElementById(\'box\');
    div = window.parent.         


        
相关标签:
4条回答
  • 2020-12-17 09:54

    There is a problem with one of your lines here; I've corrected it for you:

     var throwConnectBox = function() {
         chat_box = document.getElementById('box');
         div = window.parent.document.createElement('div');
         input = window.parent.document.createElement('input');
         input.type = "submit";
         input.value = "Join chat";
         /* this line is incorrect, surely you don't want to create a string? */
         // input.onclick = "conn.send('$connect\r\n');";?
         input.onclick = function() { 
             conn.send('$connect\r\n'); 
         };
         div.appendChild(input);
         chat_box.appendChild(div);
     }
    

    Does that make more sense?

    0 讨论(0)
  • 2020-12-17 09:59

    Try this:

     input.onclick = function() { conn.send('$connect\r\n'); };
    

    Steve

    0 讨论(0)
  • 2020-12-17 10:00

    This is one of the reasons why I've decided to use jQuery:

     $('<input type="submit" value="Join chat" />')
          .click( function() { conn.send('$connect\r\n'); } )
          .appendTo('<div></div>')
          .appendTo('#box');
    
    0 讨论(0)
  • 2020-12-17 10:05

    I think you may want to escape the \r\n, if you intend to pass these...

    conn.send('$connect\\r\\n')
    

    I don't quite see what your onclick handler tries to achieve...

    0 讨论(0)
提交回复
热议问题