<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>表單操作</title> <script type="text/javascript" src="jquery-1.10.1.js"></script> <script type="text/javascript"> $(function() {//添加新的option,設定selected為selected$("#province").append("<option value='4' selected='selected'>河南省</option>"); }); </script> </head> 使用者名稱:<input type="text" name="username" id="username"/><br/> 密碼 :<input type="password" name="password" id="password"/><br/> 性別 :<input type="radio" name="sex">男 <input type="radio" name="sex">女<br/> 愛好 :<input type="checkbox" name="interest" value="Dota">Dota <input type="checkbox" name="interest" value="LOL">LOL<input type="checkbox" name="interest" value="夢三國">夢三國<input type="checkbox" name="interest" value="英雄三國">英雄三國<br/> 省份:<select id="province"> <option>請選擇省份</option> <option value="1">黑龍江</option> <option value="2">河北</option> <option value="3">山東</option> </select><br> <input type="button" value="click" id="btn"> <body> </body></html>
2.prepend方法和append方法類似,只是prepend是在向前添加。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title></title> <script type="text/javascript" src="jquery-1.10.1.js"></script> <script type="text/javascript"> $(function() {$("p").after("<b>niujiabin<b>"); }); </script> </head> <p>this is a para</p> <body> </body></html>
運行結果:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title></title> <script type="text/javascript" src="jquery-1.10.1.js"></script> <script type="text/javascript"> $(function() {$("p").wrap("<div class='wrap' style='color:red'></div>"); }); </script> </head> <p >this is a para</p> <body> </body></html>
運行結果:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>表單操作</title> <script type="text/javascript" src="jquery-1.10.1.js"></script> <script type="text/javascript"> $(function() {$("p").wrap("<div class='wrap' style='color:red'></div>");$("p").unwrap(); }); </script> </head> <p >this is a para</p> <body> </body></html>