jQuery Mobile 搭配 HTML5 可以直接開發 Web 版的 App,拿來寫自家 App 的推廣註冊頁非常合適。如果你已經熟悉 HTML4 + jQuery 的組合,那麼換成 HTML5 + jQuery Mobile 幾乎可以立刻上手。
HTML5 比 HTML4 多了許多標籤,多媒體方面的支援尤其完整。不過若只是做一般的手機網頁,多數新標籤其實用不上。jQuery Mobile 與 jQuery 的差異主要在一些事件名稱上,而這些其實就是 HTML5 的原生事件包裝。
另外要提的是 HTML5 提倡的一條規則,而 jQuery Mobile 也遵循了它:為標籤加上新的角色屬性,例如 data-role="page"、data-role="header"、data-role="content"、data-role="footer"。我認為這些設計多半是為了支援 Web 版 App 的開發——HTML5 的做法是只建立一個 HTML 檔,然後每個區段就是一個「頁面」。
<!-- data-role="page" 表示這是一個獨立的頁面區塊 --> <div data-role="page" id="phoneRegister"> <!-- header 相當於 App 的 navigation bar --> <div data-role="header" id="heardId"></div> <!-- content 放頁面主體內容 --> <div data-role="content"></div> <!-- footer 相當於 App 的 tab bar --> <div data-role="footer"></div> </div>
這樣一組 div 就構成了一個完整的 HTML5 行動頁面。
1. 文件型別宣告。HTML5 的第一行與 HTML4 不同:
<!-- HTML5 --> <!DOCTYPE> <!-- HTML4 --> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
這個標籤的作用是告訴瀏覽器要套用哪一套 HTML 規範。
2. 引入 jQuery 與 jQuery Mobile。
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" /> <link rel="stylesheet" href="https://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" /> <script src="https://code.jquery.com/jquery-1.8.3.min.js"></script> <script src="https://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
範例直接用 CDN 連結引入,把整段程式複製成一個 html 檔開啟就能跑。
<!DOCTYPE>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
<script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="https://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
<title>App 推廣註冊頁</title>
<script type="text/javascript">
// 頁面建立完成後:依視窗高度配置 header / content / footer
$(document).on("pagecreate", function () {
var contentHeight = document.body.clientHeight;
var header = contentHeight * 0.1;
var content = contentHeight * 0.75;
var footer = contentHeight * 0.15;
$("#phoneRegister #heardId").css({
'height': header + 'px', 'background': '#4dbb49', 'line-height': header + 'px'
});
$("#phoneRegister #contentId").css({ 'height': content + 'px' });
$("#phoneRegister #footerId").css({ 'height': footer + 'px', 'line-height': footer + 'px' });
// 標題文字閃爍,吸引注意
var flag = false;
setInterval(function () {
$("#presentGiftTitle").css({ 'color': flag ? 'rgb(255,102,0)' : 'rgb(255,0,0)' });
flag = !flag;
}, 100);
});
// 進入第二個頁面時同樣配置版面
$(document).on("pageshow", "#commonRegister", function () {
var contentHeight = document.body.clientHeight;
var header = contentHeight * 0.1;
var content = contentHeight * 0.75;
var footer = contentHeight * 0.15;
$("#commonRegister #heardId").css({
'height': header + 'px', 'background': '#4dbb49', 'line-height': header + 'px'
});
$("#commonRegister #contentId").css({ 'height': content + 'px' });
$("#commonRegister #footerId").css({ 'height': footer + 'px', 'line-height': footer + 'px' });
});
// 手機註冊頁的送出驗證
$(document).on("pageinit", "#phoneRegister", function () {
$("#phoneRegisterClick").on("tap", function () {
var name = $("#phoneRegister #name").val();
var password = $("#phoneRegister #password").val();
var re = /^(13[0-9]{9})|(15[89][0-9]{8})$/;
if (!re.test(name)) { alert("請輸入正確的手機號!"); return; }
if (password.length < 6) { alert("密碼不能小於6位數"); return; }
if (!$("#phoneRegister #Checkbox1").is(":checked")) { alert("請閱讀條例!"); return; }
shareAjaxMethord(0, name, password, '', '');
});
});
// 普通註冊頁的送出驗證
$(document).on("pageinit", "#commonRegister", function () {
$("#commonRegisterClick").on("tap", function () {
var name = $("#commonRegister #name").val();
var password = $("#commonRegister #password").val();
var phone = $("#commonRegister #phoneNumber").val();
var mail = $("#commonRegister #safeMail").val();
var re = /^(13[0-9]{9})|(15[89][0-9]{8})$/;
var mre = /^\w+@\w*\.\w+$/;
if (name.length < 6) { alert("註冊用戶名不能小於6位數!"); return; }
if (password.length < 10) { alert("密碼不能小於10位數"); return; }
if (!re.test(phone)) { alert("請輸入正確的手機號!"); return; }
if (!mre.test(mail)) { alert("請輸入正確的郵箱!"); return; }
if (!$("#commonRegister #Checkbox1").is(":checked")) { alert("請閱讀條例!"); return; }
shareAjaxMethord(0, name, password, phone, mail);
});
});
function shareAjaxMethord(index, name, password, phone, email) {
$.ajax({
type: "POST",
dataType: "text",
data: "sgin=" + index + "&name=" + encodeURIComponent(name)
+ "&password=" + encodeURIComponent(password)
+ "&phone=" + encodeURIComponent(phone)
+ "&email=" + encodeURIComponent(email),
url: "ajaxMediumPage.ashx",
success: function (returnDate) {
if (index == 0) {
alert(returnDate == "1" ? "註冊成功!" : "註冊失敗!");
}
},
error: function () { alert("網路異常,請稍後再試"); }
});
}
</script>
</head>
<body>
<!-- 頁面一:手機註冊 -->
<div data-role="page" id="phoneRegister">
<div data-role="header" id="heardId">
<div style="margin-left:20px;font-size:17px;font-weight:bold">
<a href="#commonRegister" style="color:white;text-decoration:none;">首頁 > 手機在線註冊</a>
</div>
</div>
<div data-role="content" id="contentId">
<section id="posts"><article class="post">
<header><h4 id="presentGiftTitle">使用手機註冊後立刻贈送 100 樂幣!</h4></header>
<aside>手機號: <input id="name" type="tel" placeholder="請輸入手機號" /></aside>
<aside>密 碼: <input id="password" type="password" placeholder="請輸入密碼" /></aside>
<aside>
<input id="Checkbox1" type="checkbox" />
<span>打勾註冊表示你同意本公司的服務條例</span>
</aside>
<aside style="margin-top:20px;">
<div id="phoneRegisterClick"
style="text-align:center;border:1px solid #666;background:#4dbb49;color:#fff;
height:30px;width:100px;line-height:30px;">立即註冊</div>
</aside>
<aside style="margin-top:20px;">
<a href="#commonRegister" data-transition="flip">普通註冊(非手機)</a>
</aside>
</article></section>
</div>
<div data-role="footer" id="footerId" style="background:#666;">
<div style="text-align:center;font-size:13px;">
<div>電話:400-900-8000</div>
<div>渝 ICP 備 000000-c 號</div>
<div>版權所有</div>
</div>
</div>
</div>
<!-- 頁面二:普通註冊 -->
<div data-role="page" id="commonRegister">
<div data-role="header" id="heardId">
<div style="margin-left:20px;font-size:17px;font-weight:bold">
<a href="#phoneRegister" style="color:white;text-decoration:none;">首頁 > 普通用戶在線註冊</a>
</div>
</div>
<div data-role="content" id="contentId">
<section id="posts"><article class="post">
<aside>帳 號: <input id="name" type="text" placeholder="請輸入註冊帳號" /></aside>
<aside>密 碼: <input id="password" type="password" placeholder="請輸入密碼" /></aside>
<aside>手機號: <input id="phoneNumber" type="tel" placeholder="請輸入手機號" /></aside>
<aside>安全郵箱: <input id="safeMail" type="email" placeholder="請輸入安全郵箱" /></aside>
<aside>
<input id="Checkbox1" type="checkbox" />
<span>打勾註冊表示你同意本公司的服務條例</span>
</aside>
<aside style="margin-top:20px;">
<div id="commonRegisterClick"
style="text-align:center;border:1px solid #666;background:#4dbb49;color:#fff;
height:30px;width:100px;line-height:30px;">立即註冊</div>
</aside>
<aside style="margin-top:20px;">
<a href="#phoneRegister" data-transition="flip">手機註冊(手機號)</a>
</aside>
</article></section>
</div>
<div data-role="footer" id="footerId" style="background:#666;">
<div style="text-align:center;font-size:13px;">
<div>電話:400-900-8000</div>
<div>渝 ICP 備 000000-c 號</div>
<div>版權所有</div>
</div>
</div>
</div>
</body>
</html>
1. 用 data-role 的頁面結構取代百分比排版。範例用 clientHeight 的百分比去算 header、content、footer 的高度,在不同裝置上容易出錯。jQuery Mobile 本身已經處理了版面配置,通常不需要手動算;真要固定高度,用 CSS 的 flex 或 dvh 單位會更穩。
2. 同一頁面上 id 必須唯一。範例中兩個 page 區塊都用了 id="heardId"、id="contentId"、id="Checkbox1",這在 HTML 中是不合法的,jQuery 選取也只會命中第一個。應該加上頁面前後綴區分。
3. 表單不要用 GET 傳密碼。範例把帳密用查詢字串 POST 出去,且沒有 encodeURIComponent。正式環境請改用 data: {...} 物件形式讓 jQuery 處理編碼,並且全程走 HTTPS。
4. 避免使用 alert 做錯誤提示。jQuery Mobile 沒有內建的提示元件,建議自行以 popup 或第三方函式庫取代,行動裝置上的體驗會好很多。
整體來說,這個範例最能拿來參考的是「用多個 data-role="page" 區塊組成單頁應用」以及 pagecreate/pageshow/pageinit 這三個生命週期事件的分工:頁面物件建立時、頁面首次初始化時、每次顯示時,各自負責的事情不同,釐清這點就能避免許多半初始化狀態的怪問題。