1. 程式人生 > >jQuery 建立和插入元素

jQuery 建立和插入元素

原文連結

 

 

一、使用jQuery建立元素

1、建立元素

[javascript] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. $(function(){  
  2. var $h1=$(“<h1></h1>”);  
  3. $(“body”).append($h1);  
  4. })  

2、建立文字

[javascript] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. $(
    function(){  
  2. var $h1=$(“<h1>DOM文件物件模型</h1>”);  
  3. $(“body”).append($h1);  
  4. })  

3、建立屬性

[javascript] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. $(function(){  
  2. var $h1=$(“<h1 title=“一級標題”  class=“red”>DOM文件物件模型</h1>”);  
  3. $(“body”).append($h1);  
  4. })  

 

二、使用jQuery插入元素

 

1、在節點內部插入內容

 

(1)、append()方法在被選元素的結尾(仍然在內部)插入指定內容

   提示:append() 和 appendTo() 方法執行的任務相同。不同之處在於:內容的位置和選擇器。

        語法:$(selector).append(content)

   content必需。規定要插入的內容(可包含 HTML 標籤)。

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $("button").click(function(){  
  7.     $("p").append(" <b>Hello world!</b>");  
  8.   });  
  9. });  
  10. </script>  
  11. </head>  
  12. <body>  
  13. <p>This is a paragraph.</p>  
  14. <p>This is another paragraph.</p>  
  15. <button>在每個 p 元素的結尾新增內容</button>  
  16. </body>  
  17. </html>  

 

 

  使用函式來附加內容,使用函式在指定元素的結尾插入內容。

 

 

        語法:$(selector).append(function(index,html))

  function(index,html) 必需。規定返回待插入內容的函式。

  index - 可選。接收選擇器的 index 位置。

  html - 可選。接收選擇器的當前 HTML。

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $("button").click(function(){  
  7.     $("p").append(function(n){  
  8.       return "<b>This p element has index " + n + "</b>";  
  9.     });  
  10.   });  
  11. });  
  12. </script>  
  13. </head>  
  14. <body>  
  15. <h1>This is a heading</h1>  
  16. <p>This is a paragraph.</p>  
  17. <p>This is another paragraph.</p>  
  18. <button>在每個 p 元素的結尾新增內容</button>  
  19. </body>  
  20. </html>  


 

 

 

(2)、appendTo()方法在被選元素的結尾(仍然在內部)插入指定內容

  提示:append() 和 appendTo() 方法執行的任務相同。不同之處在於:內容和選擇器的位置,以及 append() 能夠使用函式來附加內容。

       語法:$(content).appendTo(selector)

  content必需。規定要插入的內容(可包含 HTML 標籤)。

  selector必需。規定把內容追加到哪個元素上。

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $("button").click(function(){  
  7.     $("<b> Hello World!</b>").appendTo("p");  
  8.   });  
  9. });  
  10. </script>  
  11. </head>  
  12. <body>  
  13. <p>This is a paragraph.</p>  
  14. <p>This is another paragraph.</p>  
  15. <button>在每個 p 元素的結尾新增內容</button>  
  16. </body>  
  17. </html>  

 

 

(3)、prepend() 方法在被選元素的開頭(仍位於內部)插入指定內容

 

  提示:prepend() 和 prependTo() 方法作用相同。差異在於語法:內容和選擇器的位置,以及 prependTo() 無法使用函式來插入內容。

        語法:$(selector).prepend(content)

  content必需。規定要插入的內容(可包含 HTML 標籤)。

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $("button").click(function(){  
  7.     $("p").prepend("<b>Hello world!</b> ");  
  8.   });  
  9. });  
  10. </script>  
  11. </head>  
  12. <body>  
  13. <p>This is a paragraph.</p>  
  14. <p>This is another paragraph.</p>  
  15. <button>在每個 p 元素的開頭插入內容</button>  
  16. </body>  
  17. </html>  


 

  使用函式來附加內容,使用函式在被選元素的開頭插入指定的內容。

 

        語法:$(selector).prepend(function(index,html))

  function(index,html) 必需。規定返回待插入內容的函式。

  index - 可選。接受選擇器的 index 位置。

  html - 可選。接受選擇器的當前 HTML。

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $("button").click(function(){  
  7.     $("p").prepend(function(n){  
  8.       return "<b>這個 p 元素的 index 是:" + n + "</b> ";  
  9.     });  
  10.   });  
  11. });  
  12. </script>  
  13. </head>  
  14. <body>  
  15. <h1>這是一個標題</h1>  
  16. <p>這是一個段落。</p>  
  17. <p>這是另一個段落。</p>  
  18. <button>在每個 p 元素的開頭插入內容</button>  
  19. </body>  
  20. </html>  

 

 

 

 

 

(4)、prependTo() 方法在被選元素的開頭(仍位於內部)插入指定內容

  提示:prepend() 和 prependTo() 方法作用相同。差異在於語法:內容和選擇器的位置,以及 prepend() 能夠使用函式來插入內容。

         語法:$(content).prependTo(selector)

  content 必需。規定要插入的內容(可包含 HTML 標籤)。

  selector 必需。規定在何處插入內容。

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $(".btn1").click(function(){  
  7.     $("<b>Hello World!</b>").prependTo("p");  
  8.   });  
  9. });  
  10. </script>  
  11. </head>  
  12. <body>  
  13. <p>This is a paragraph.</p>  
  14. <p>This is another paragraph.</p>  
  15. <button class="btn1">在每個 p 元素的開頭插入文字</button>  
  16. </body>  
  17. </html>  


 

 

 

2、在節點外部插入內容

(1)、after() 方法在被選元素後插入指定的內容

       語法:$(selector).after(content)

   content必需。規定要插入的內容(可包含 HTML 標籤)。

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $("button").click(function(){  
  7.     $("p").after("<p>Hello world!</p>");  
  8.   });  
  9. });  
  10. </script>  
  11. </head>  
  12. <body>  
  13. <p>This is a paragraph.</p>  
  14. <button>在每個 p 元素後插入內容</button>  
  15. </body>  
  16. </html>  


 

 

 

  使用函式來插入內容,使用函式在被選元素之後插入指定的內容。

        語法:$(selector).after(function(index))

  function(index) 必需。規定返回待插入內容的函式。

  index - 可選。接收選擇器的 index 位置。

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $("button").click(function(){  
  7.     $("p").after(function(n){  
  8.       return "<p>The p element above has index " + n + "</p>";  
  9.     });  
  10.   });  
  11. });  
  12. </script>  
  13. </head>  
  14. <body>  
  15. <h1>This is a heading</h1>  
  16. <p>This is a paragraph.</p>  
  17. <p>This is another paragraph.</p>  
  18. <button>在每個 p 元素後插入內容</button>  
  19. </body>  
  20. </html>  


 

 

 

(2)、before() 方法在被選元素前插入指定的內容

        語法:$(selector).before(content)

    content 必需。規定要插入的內容(可包含 HTML 標籤)。

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $(".btn1").click(function(){  
  7.     $("p").before("<p>Hello world!</p>");  
  8.   });  
  9. });  
  10. </script>  
  11. </head>  
  12. <body>  
  13. <p>This is a paragraph.</p>  
  14. <button class="btn1">在每個段落前面插入新的段落</button>  
  15. </body>  
  16. </html>  


 

  使用函式來插入內容,使用函式在指定的元素前面插入內容。

 

     語法:$(selector).before(function(index))

  function(index)必需。規定返回待插入內容的函式。

  index - 可選。接收選擇器的 index 位置。

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $("button").click(function(){  
  7.     $("p").before(function(n){  
  8.       return "<p>The p element below has index " + n + "</p>";  
  9.     });  
  10.   });  
  11. });  
  12. </script>  
  13. </head>  
  14. <body>  
  15. <h1>This is a heading</h1>  
  16. <p>This is a paragraph.</p>  
  17. <p>This is another paragraph.</p>  
  18. <button class="btn1">在每個段落前面插入新的段落</button>  
  19. </body>  
  20. </html>  


 

 

 

(3)、insertAfter()把匹配的元素插入到另一個指定的元素集合的後面

  註釋:如果該方法用於已有元素,這些元素會被從當前位置移走,然後被新增到被選元素之後。

       語法:$(content).insertAfter(selector)

  content必需。規定要插入的內容。可能的值:選擇器表示式、HTML 標記

  selector必需。規定在何處插入被選元素。

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $("button").click(function(){  
  7.     $("<span>你好!</span>").insertAfter("p");  
  8.   });  
  9. });  
  10. </script>  
  11. </head>  
  12. <body>  
  13. <p>這是一個段落。</p>  
  14. <p>這是另一個段落。</p>  
  15. <button>在每個 p 元素之後插入 span 元素</button>  
  16. </body>  
  17. </html>  


 

插入已有的元素

 

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $("button").click(function(){  
  7.     $("h1").insertAfter("p");  
  8.   });  
  9. });  
  10. </script>  
  11. </head>  
  12. <body>  
  13. <h1>這是一個標題</h1>  
  14. <p>這是一個段落。</p>  
  15. <p>這是另一個段落。</p>  
  16. <button>在每個 p 元素之後插入 h1 元素</button>  
  17. </body>  
  18. </html>  


 

 

 

(4)、insertBefore()把匹配的元素插入到另一個指定的元素集合的前面

   註釋:如果該方法用於已有元素,這些元素會被從當前位置移走,然後被新增到被選元素之前。

        語法:$(content).insertBefore(selector)

   content 必需。規定要插入的內容。可能的值:選擇器表示式、HTML 標記

   selector 必需。規定在何處插入被選元素。

[html] view plain copy 在CODE上檢視程式碼片派生到我的程式碼片

  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $("button").click(function(){  
  7.     $("<span>你好!</span>").insertBefore("p");  
  8.   });  
  9. });  
  10. </script>  
  11. </head>  
  12. <body>  
  13. <p>這是一個段落。</p>  
  14. <p>這是另一個段落。</p>  
  15. <button>在每個 p 元素之前插入 span 元素</button>  
  16. </body>  
  17. </html>