jQuery動態建立html元素的常用方法匯總_jquery

來源:互聯網
上載者:User

本文執行個體講述了jQuery動態建立html元素的常用方法,在使用jQuery進行WEB程式設計的時候非常有用。分享給大家供大家參考。具體方法如下:

一般來說,可以通過以下幾種方式動態建立html元素:

1、使用jQuery建立元素的文法
2、把動態內容存放到數組中,再遍曆數組動態建立html元素
3、使用模版

1.使用jQuery動態建立元素追加到jQuery對象上。

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>   <title></title>   <script src="Scripts/jquery-1.10.2.js"></script>   <script type="text/javascript">     $(function() {       $('<input />', {         id: 'cbx',         name: 'cbx',         type: 'checkbox',         checked: 'checked',         click: function() {           alert("點我了~~");         }       }).appendTo($('#wrap'));     });   </script> </head> <body>   <div id="wrap"></div> </body>

運行效果如下圖所示:

2.先把內容放到數組中,然後遍曆數組拼接成html

 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>   <title></title>   <script src="Scripts/jquery-1.10.2.js"></script>   <style type="text/css">     table {       border: solid 1px red;       border-collapse: collapse;     }       td {       border: solid 1px red;     }   </style>   <script type="text/javascript">     $(function () {       var data = ["a", "b", "c", "d"];       var html = '';       for (var i = 0; i < data.length; i ++) {         html += "<td>" + data[i] + "</td>";       }       $("#row").append(html);     });   </script> </head> <body>   <table>     <tr id="row"></tr>   </table> </body> </html>

運行效果如下圖所示:

3.使用模版產生html

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>   <title></title>   <script src="Scripts/jquery-1.10.2.js"></script>   <script type="text/javascript">     $(function () {       var a = buildHTML("a", "我是由模版產生的", {         id: "myLink",         href: "http://www.baidu.com"       });         $('#wrap1').append(a);         var input = buildHTML("input", {         id: "myInput",         type: "text",         value: "我也是由模版產生的~~"       });         $('#wrap2').append(input);     });       buildHTML = function(tag, html, attrs) {       // you can skip html param       if (typeof (html) != 'string') {         attrs = html;         html = null;       }       var h = '<' + tag;       for (attr in attrs) {         if (attrs[attr] === false) continue;         h += ' ' + attr + '="' + attrs[attr] + '"';       }       return h += html ? ">" + html + "</" + tag + ">" : "/>";     };   </script> </head> <body>   <div id="wrap1"></div>   <div id="wrap2"></div> </body>

運行效果如下圖所示:

相信本文所述對大家使用jQuery進行WEB程式設計有一定的借鑒價值。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.