เริ่มต้นกับ jQuery Mobile
jQuery Mobile จะช่วยให้ใช้งานระบบได้ง่ายขึ้น และยังใช้งานร่วมกับระบบนำทางAJAX
ที่สามารถรองรับหน้าเพจที่มีการเคลื่อนไหวได้อีกด้วยสามารถเรียกใช้ jQuery Mobileไว้ในส่วนของ head ซึ่งสามารถเรียกใช้ได้ 2 วิธี คือ
1.เรียกใช้จากเว็บไซต์ของ jQuery Mobile
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
2.เรียกใช้จากไฟล์ในคอมพิวเตอร์
<link rel="stylesheet" href="css/jquery.mobile-1.2.0.min.css"/>
<script src="js/jquery-1.8.2.min.js"></script>
<script src="js/jquery.mobile-1.2.0.min.js"></script>
การสร้าง page template เบื้องต้น
ทดลองสร้าง page template สามารถทำได้โดย สร้างไฟล์ html หรือ php ขึ้นมา 1ไฟล์ และวางโค้ดตัวอย่างลงในไฟล์ หลังจากนั้น save และทดลองเปิดในเบราว์เซอร์
<head>
<title>My Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-
1.2.0.min.css"
/>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js">
</script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>My Title</h1>
</div><!-- /header -->
<div data-role="content">
<p>Hello World</p>
</div><!-- /content -->
<div data-role="footer">
<p> Footer </p>
</div><!-- /content -->
</div><!-- /page -->
</body>
สิ่งที่อยู่ใน template คือ <head> และ<body>
หมายเลข 1 ในส่วนของ <head>
<meta> คือการกำหนดความกว้างพิกเซลของหน้าจออุปกรณ์ โดยอ้างอิงถึงรูปแบบและสคริปต์
ของ jQuery,jQuery Mobile และ mobile theme stylesheet จาก CDN ซึ่ง jQuery Mobile 1.2 (1.2.0) ทำงานร่วมกับ jQuery core ได้ตั้งแต่เวอร์ชัน 1.7.0 ถึง 1.8.2
หมายเลข 2 ในส่วนของ <body>
<div data-role="page"> เป็นการกำหนดพื้นที่ทั้งหมดของเนื้อหาจะแสดงบนหน้าเว็บไซต์
โดย data-role="header"คือส่วนของ header bar และdata-role="content"คือเนื้อหาของเว็บ ซึ่งdata- attributes เหล่านี้เป็นแอตทริบิวต์ของ HTML5 ซึ่ง jQuery Mobile นำมาใช้เพื่อปรับเปลี่ยน
มาร์กอัป(markup)พื้นฐานและรูปแบบของwidget
การเพิ่ม content
ในส่วนที่เป็นข้อมูลสามารถเพิ่มHTML elements อื่นๆได้ เช่นเพิ่ม headings, lists, paragraphs ซึ่งสามารถสร้าง layouts ในรูปแบบของตนเองได้ โดยการเพิ่ม stylesheet ไปยัง head ต่อจาก stylesheet ของ jQuery Mobile
การทำ listview
jQuery Mobile สามารถ includes common listviews โดยเพิ่มแอตทริบิวต์ที่ชื่อว่า
data-role="listview" อีกทั้งยังสามารถเพิ่มแอตทริบิวต์ data-inset="true" และเพิ่มการกรองการค้นหาแบบไดนามิกด้วยแอตทริบิวต์ data-filter="true" ดังตัวอย่างต่อไปนี้
CD ไฟล์ : linked_list.html
เกร็ดความรู้ :เมื่อเพิ่มแถบธีมลงในเพจจะสังเกตเห็นได้ว่า ภายในcontentจะเปลี่ยนแปลงไป แบบอัตโนมัติ แต่ในส่วนของ header และ footer จะไม่มีเปลี่ยนแปลง โดยมีค่าเริ่มต้นของแถบคือ "a"