PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > Jquery教程 > 正文

    jQuery 入门教程: 添加HTML元素

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:使用jQuery可以方便的添加新的HTML元素。下面的方法用于添加HTML元素:append() – 在指定的元素的尾部添加一个新内容。prepend() ...
    使用jQuery可以方便的添加新的HTML元素。
    下面的方法用于添加HTML元素:

    append() – 在指定的元素的尾部添加一个新内容。
    prepend() -在指定的元素里前部添加新内容。
    after() – 在指定元素前添加新内容
    before() -在指定元素的后面添加新内容。
    乍一看append,prepend 和after,before似乎功能一样,但append,prepend指在选中的元素本身(内部)的前面和后面,而after,before指在选择中的元素的前面和后面。

    可以参考下面的append例子:


    [html] 
    <!DOCTYPE html> 
    <html> 
    <head> 
        <meta charset="utf-8"> 
        <title>JQuery Demo</title> 
        <script src="scripts/jquery-1.9.1.js"></script> 
        <script> 
            $(document).ready(function () { 
                $("#btn1").click(function () { 
                    $("p").append(" <b>Appended text</b>."); 
                }); 
     
                $("#btn2").click(function () { 
                    $("ol").append("<li>Appended item</li>"); 
                }); 
            }); 
        </script> 
    </head> 
     
    <body> 
        <p>This is a paragraph.</p> 
        <p>This is another paragraph.</p> 
        <ol> 
            <li>List item 1</li> 
            <li>List item 2</li> 
            <li>List item 3</li> 
        </ol> 
        <button id="btn1">Append text</button> 
        <button id="btn2">Append list items</button> 
    </body> 
    </html> 

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>JQuery Demo</title>
        <script src="scripts/jquery-1.9.1.js"></script>
        <script>
            $(document).ready(function () {
                $("#btn1").click(function () {
                    $("p").append(" <b>Appended text</b>.");
                });

                $("#btn2").click(function () {
                    $("ol").append("<li>Appended item</li>");
                });
            });
        </script>
    </head>

    <body>
        <p>This is a paragraph.</p>
        <p>This is another paragraph.</p>
        <ol>
            <li>List item 1</li>
            <li>List item 2</li>
            <li>List item 3</li>
        </ol>
        <button id="btn1">Append text</button>
        <button id="btn2">Append list items</button>
    </body>
    </html>





    prepend示例:

    [html] 
    <!DOCTYPE html> 
    <html> 
    <head> 
        <meta charset="utf-8"> 
        <title>JQuery Demo</title> 
        <script src="scripts/jquery-1.9.1.js"></script> 
        <script> 
            $(document).ready(function () { 
                $("#btn1").click(function () { 
                    $("p").prepend("<b>Prepended text</b>. "); 
                }); 
                $("#btn2").click(function () { 
                    $("ol").prepend("<li>Prepended item</li>"); 
                }); 
            }); 
        </script> 
    </head> 
    <body> 
     
        <p>This is a paragraph.</p> 
        <p>This is another paragraph.</p> 
        <ol> 
            <li>List item 1</li> 
            <li>List item 2</li> 
            <li>List item 3</li> 
        </ol> 
     
        <button id="btn1">Prepend text</button> 
        <button id="btn2">Prepend list item</button> 
     
    </body> 
    </html> 

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>JQuery Demo</title>
        <script src="scripts/jquery-1.9.1.js"></script>
        <script>
            $(document).ready(function () {
                $("#btn1").click(function () {
                    $("p").prepend("<b>Prepended text</b>. ");
                });
                $("#btn2").click(function () {
                    $("ol").prepend("<li>Prepended item</li>");
                });
            });
        </script>
    </head>
    <body>

        <p>This is a paragraph.</p>
        <p>This is another paragraph.</p>
        <ol>
            <li>List item 1</li>
            <li>List item 2</li>
            <li>List item 3</li>
        </ol>

        <button id="btn1">Prepend text</button>
        <button id="btn2">Prepend list item</button>

    </body>
    </html>




    append(),prepend()支持同时插入多个元素,下面的例子添加三个使用不同方法创建的新元素:

    [html]
    function appendText() 

    // Create element with HTML 
    var txt1="<p>Text.</p>"; 
    // Create with jQuery 
    var txt2=$("<p></p>").text("Text."); 
    // Create with DOM 
    var txt3=document.createElement("p"); 
    txt3.innerHTML="Text."; 
    // Append the new elements 
    $("p").append(txt1,txt2,txt3); 


    function appendText()
    {
    // Create element with HTML
    var txt1="<p>Text.</p>";
    // Create with jQuery
    var txt2=$("<p></p>").text("Text.");
    // Create with DOM
    var txt3=document.createElement("p");
    txt3.innerHTML="Text.";
    // Append the new elements
    $("p").append(txt1,txt2,txt3);
    }

    下面的例子使用after,before 在指定的元素前后面添加新内容:


    [html] 
    <!DOCTYPE html> 
    <html> 
    <head> 
        <meta charset="utf-8"> 
        <title>JQuery Demo</title> 
        <script src="scripts/jquery-1.9.1.js"></script> 
        <script> 
            $(document).ready(function () { 
                $("#btn1").click(function () { 
                    $("img").before("<b>Before</b>"); 
                }); 
     
                $("#btn2").click(function () { 
                    $("img").after("<i>After</i>"); 
                }); 
            }); 
        </script> 
    </head> 
     
    <body> 
        <img src="/images/guidebee.png" 
            alt="guidebee" width="120" height="125"> 
        <br> 
        <br> 
        <button id="btn1">Insert before</button> 
        <button id="btn2">Insert after</button> 
    </body> 
    </html> 

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>JQuery Demo</title>
        <script src="scripts/jquery-1.9.1.js"></script>
        <script>
            $(document).ready(function () {
                $("#btn1").click(function () {
                    $("img").before("<b>Before</b>");
                });

                $("#btn2").click(function () {
                    $("img").after("<i>After</i>");
                });
            });
        </script>
    </head>

    <body>
        <img src="/images/guidebee.png"
            alt="guidebee" width="120" height="125">
        <br>
        <br>
        <button id="btn1">Insert before</button>
        <button id="btn2">Insert after</button>
    </body>
    </html>

    同样after,before也支持同时插入多个元素:



    [html] 
    function afterText() 

      // Create element with HTML  
    var txt1="<b>I </b>";    
    // Create with jQuery  
    var txt2=$("<i></i>").text("love ");    
    // Create with DOM  
    var txt3=document.createElement("big");   
    txt3.innerHTML="jQuery!"; 
      // Insert new elements after img 
    $("img").after(txt1,txt2,txt3);          


    function afterText()
    {
      // Create element with HTML
    var txt1="<b>I </b>";  
    // Create with jQuery
    var txt2=$("<i></i>").text("love ");  
    // Create with DOM
    var txt3=document.createElement("big"); 
    txt3.innerHTML="jQuery!";
      // Insert new elements after img
    $("img").after(txt1,txt2,txt3);        
    }
    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-47-1420-1.html
    相关热词搜索: jQuery教程