使用OnClick在文本框中显示消息

我想在一个文本框上添加onClick(),该文本框是使用以下Java脚本方法在表中动态生成的.我只想调用一个Java脚本方法,它将通过此代码生成.
假设我想要在任何人点击用此代码生成的文本框时显示一条消息"hi".

选择 | 换行 | 行号
  1.   function addRow()
  2.   {
  3.  
  4.  var tbody = document.getElementById("table2").getElementsByTagName("tbody")[0]; 
  5. var row = document.createElement("TR"); 
  6. var cell1 = document.createElement("TD"); 
  7. var inp1 =  document.createElement("INPUT"); 
  8. inp1.setAttribute("type","text"); 
  9. inp1.setAttribute("name","game_"+count);
  10. inp1.setAttribute("id","game_"+count);
  11. inp1.setAttribute("maxlength",11);
  12. inp1.setAttribute("size",10);
  13. inp1.setAttribute("value",document.forms.form1.elements.game.value); 
  14. cell1.appendChild(inp1); 
  15. var cell2 = document.createElement("TD"); 
  16. var inp2 =  document.createElement("INPUT");
  17. inp2.setAttribute("type","text"); 
  18. inp2.setAttribute("name","number_"+count);
  19. inp2.setAttribute("id","number_"+count);
  20. inp2.setAttribute("maxlength",11);
  21. inp2.setAttribute("size",10);
  22. cell2.appendChild(inp2); 
  23. row.appendChild(cell1); 
  24. row.appendChild(cell2); 
  25. }
# 回答1

选择 | 换行 | 行号
  1. inp2.onclick = function() { alert('hi'); }

标签: Javascript

添加新评论