使用jQuery創建節點、將節點插入到指定的位置


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-1.11.1.js"></script>
<script type="text/javascript">
	$(function (){
		//1、創建節點
		//2、插入節點,在該節點的子節點的最后面插入
		$("<li id='shenzhen'>[shenzhen]</li>").appendTo($("#city"));
		$("#city").append("<li id='shenzhen'>shenzhen</li>");
		
		//插入結點:在該節點的子節點的最前面插入
		$("<li id='math'>數學</li>").prependTo($("#major"));
		$("#major").prepend("<li id='math'>[數學]</li>");
		
		//把新創建的節點添加到指定節點之后
		$("<li id='shenzhen'>shenzhen</li>").insertAfter($("#beijing"));
		$("#beijing").after("<li id='shenzhen'>[shenzhen]</li>");
		
		//把新創建的節點添加到指定節點之前
		$("<li id='shenzhen'>shenzhen</li>").insertBefore($("#beijing"));
		$("#beijing").before($("<li id='shenzhen'>[shenzhen]</li>"));
		
	});
</script>
</head>
<body>
	<p>選擇你所喜歡的城市?</p>
	<ul id="city">
		<li>哈爾濱</li>
		<li id="beijing">北京</li>
		<li>上海</li>
		<li>廣州</li>
	</ul>
	<p>選擇你所喜歡的專業?</p>
	<ul id="major">
		<li>計算機</li>
		<li>會計</li>
		<li>管理學</li>
		<li>印刷工程</li>
	</ul>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM