1. append() 在结尾处添加元素
<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <script type="text/javascript" src="js/jquery-1.11.3.js"></script> <script> $(function(){ $("#btn1").click(function(){ $("p").append("<b>Append text</b>."); }); $("#btn2").click(function(){ $("ol").append("<li>Append item.</li>") }) }); </script> </head> <body> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <ol> <li>List item1</li> <li>List item2</li> <li>List item3</li> </ol> <button id="btn1">追加文本</button> <button id="btn2">追加列表项</button> </body> </html>
2. prepend()在前面加入
<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <script type="text/javascript" src="js/jquery-1.11.3.js"></script> <script> $(function(){ $("#btn1").click(function(){ $("p").append("<b>Append text</b>."); }); $("#btn2").click(function(){ $("ol").append("<li>Append item.</li>"); }); $("#btn3").click(function(){ $("p").prepend("<b>prepend text</b>"); }); $("#btn4").click(function(){ $("ol").prepend("<li>prepend text.</li>") }) }); </script> </head> <body> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <ol> <li>List item1</li> <li>List item2</li> <li>List item3</li> </ol> <button id="btn1">追加文本</button> <button id="btn2">追加列表项</button> <button id="btn3">在前面加入文本</button> <button id="btn4">在前面列表项</button> </body> </html>