现在的位置: 首页 > 综合 > 正文

动态创建DOM对象

2013年10月06日 ⁄ 综合 ⁄ 共 725字 ⁄ 字号 评论关闭
   <script type="text/javascript">
    //在javascript里面创建
    function q(){
        document.getElementById("btn1").onclick=function(){
         var link=document.createElement("a");
         link.setAttribute("href","http://www.baidu.com");
         link.innerHTML="<font color='red'>你好啊</font>";
         document.getElementById("main").appendChild(link);
         
        };
     }
    </script>
    
    <script type="text/javascript">
      $(function(){
        $("#btn2").click(function(){
          var link1 = $("<a href='http://www.baidu.com'><font color='red'>你好啊</font></a>");//$是根据一段html代码生成jquery对象
         $("#main").append(link1);//把jquery对象附加到body上
         for(var i=0;i<10;i++){//创建文本框
         var input=$("<input type='text' />");
         input.val(i);
         $("#main").append(input);
         $(":text").css("background","red");
         }
        });
      });
    </script>
    

一种是利用javascript比较麻烦的哦,还是用jquery来的直接$(html内容),直接append就可以了

抱歉!评论已关闭.