jQuery HTML之添加元素
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="../Scripts/jquery-3.4.1.min.js"></script> <script src="AddContent.js"></script> </head> <body> <p id="p1">hello world</p> <p id="p2">hello world</p> <button id="btn1"></button> <button id="btn2"></button> <button onclick="appendText()">按钮</button> </body> </html>
$(document).ready(function () { $("#btn1").click(function () { $("#p1").append("this is my webpage add content, "); //不换行 $("#p1").append("this is my webpage add content, "); }); $("#btn2").click(function () { $("#p2").before("hello");//换行 $("#p2").after("hello"); }); }); function appendText() { /** * Html、Jquery、DOM三种方式追加内容 */ var text1 = "<p>iwen</p>"; var text2 = $("<p></p>").text("ime");//找到text添加内容 var text3 = document.createElement("p"); text3.innerHTML = "acely"; $("body").append(text1, text2, text3);//追加到body中 }