คำสั่ง prepend() ของ jquery

August 2009 | By platoosom | jQuery | Tags : , | อ่าน : 217

คำสั่งนี้จะทำงานตรงกันข้ามกับคำสั่ง append() โดยมันจะเพิ่มเนื้อหาเข้าไปในส่วนต้น ของเนื้อหาเดิม

prepend(content)

content :(string) เนื้อหาที่ต้องการเพิ่มเข้าไปในส่วนต้นของเนื้อหาเดิม สามารถใช้แท็ก html ปนได้

ตัวอย่างการใช้งาน

Live Demo

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<title>jquery-prepend-command</title>
<script type="text/javascript">
$(document).ready(function(){
 
	$('#set').click(function(){
 
		var content = 'ข้อความใหม่ จะเพิ่มเข้าไปส่วนต้น ของเนื้อหาเดิม <i>เน้นๆ</i> ';
 
		$('#content').prepend( content );
 
	});
 
})
</script>
</head>
<body>
 
    <div id="content"><strong>ข้อความเดิม</strong></div>
 
      <input name="set" type="button" id="set"  value="Prepend Text" /> 
 
</body>
</html>

ข้อดีข้อหนึ่งของ jquery คือ ทำเรื่องยากให้เป็นเรื่องง่าย ครับ

บทความน่าสนใจ

  1. คำสั่ง before() ของ jquery
  2. คำสั่ง after() ของ jquery
  3. คำสั่ง text() ของ jquery
  4. คำสั่ง append() ของ jquery
  5. คำสั่ง html() ของ jquery

ผู้เขียน: วัชรเมธน์ ชิษณุคุปต์ ศรีเนธิโรทัย

เว็บไซต์ :http://www.select2web.com

งานเล็กๆก็ยิ่งใหญ่ได้ ถ้าอยู่ในที่ๆเหมาะสม

แสดงความเห็น

ชื่อ *
Email *
Website
ความคิดเห็น