Discuz教程网

JQuery动态创建DOM、表单元素的实现代码

[复制链接]
authicon dly 发表于 2011-9-12 15:16:10 | 显示全部楼层 |阅读模式
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>createElement</title>
  6. <style type="text/css">
  7. .warpper{ border:1px solid red; padding:8px;}
  8. </style>
  9. <script type="text/javascript" language="javascript" src="JavaScript/jquery-1.6.1.min.js" ></script>
  10. <script type="text/javascript" language="javascript">
  11. <!--
  12. ///动态创建一个div
  13. $(function(){
  14. $('<div />',{
  15. id:'test',
  16. text:"this is a div",
  17. "class":"warpper",
  18. click:function(){
  19. var text=$(this).text();
  20. alert(text);
  21. }
  22. }).appendTo("body");
  23. });
  24. //创建input:text
  25. $(function(){
  26. $('<input />',{
  27. type:"text",
  28. val:"input text somethings...",
  29. name:"userName"
  30. }).appendTo("body");
  31. });
  32. //创建input select
  33. $(function(){
  34. var slt=$('<select />',{name:"country" });
  35. $('<option />',{
  36. val:"0",
  37. text:"请选择"
  38. }).appendTo(slt);
  39. $('<option>',{
  40. val:"CN",
  41. text:"China",
  42. selected:"selected"
  43. }).appendTo(slt);
  44. $("body").append(slt);
  45. });
  46. //创建radio
  47. $(function(){
  48. $('<input />',{
  49. type:"radio",
  50. name:"rdo",
  51. checked:"checked",
  52. val:"男"
  53. }).appendTo("body");
  54. $('<label>',{
  55. text:"男",
  56. }).appendTo("body");
  57. $('<input />',{
  58. type:"radio",
  59. name:"rdo",
  60. val:"女"
  61. }).appendTo("body");
  62. $('<label>',{
  63. text:"女"
  64. }).appendTo("body");
  65. });
  66. //creat checkbox
  67. $(function(){
  68. $('<input />',{
  69. type:"checkbox",
  70. name:"cbox",
  71. val:"1",
  72. checked:"checked"
  73. }).appendTo("body");
  74. });
  75. $(function(){
  76. $('<input />',{
  77. type:"file",
  78. name:"myfile"
  79. }).appendTo("body");
  80. });
  81. //-->
  82. </script>
  83. </head>
  84. <body>
  85. <form>
  86. </body>
  87. </html>
复制代码




上一篇:基于Jquery的文字自动截取(提供源代码)
下一篇:用JS判断IE版本的代码 超管用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 20:09

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表