十年前我第一次接触移动端开发时,面对各种设备分辨率和操作系统版本,适配工作简直让人崩溃。直到遇到jQuery Mobile,这个基于jQuery的移动端框架用简单的HTML5标记就能实现跨平台响应式布局,当时感觉就像发现了新大陆。
如今虽然React Native、Flutter等框架大行其道,但jQuery Mobile在快速原型开发、企业后台管理系统等场景依然保持着独特优势。最新1.5版本支持所有主流移动浏览器,压缩后的核心文件仅24KB,对于需要快速交付的项目仍然是性价比极高的选择。
最快捷的方式是使用CDN引入,在HTML的
中加入以下代码:html复制<link rel="stylesheet" href="https://code.jquery.com/mobile/1.5.0/jquery.mobile-1.5.0.min.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/mobile/1.5.0/jquery.mobile-1.5.0.min.js"></script>
注意:jQuery Mobile 1.5要求jQuery版本在1.8-3.6之间,建议使用jQuery 3.x版本以获得最佳兼容性
从官网下载压缩包:
bash复制wget https://jquerymobile.com/resources/download/jquery.mobile-1.5.0.zip
unzip jquery.mobile-1.5.0.zip
项目目录结构建议:
code复制/project
/css
jquery.mobile-1.5.0.min.css
/js
jquery-3.6.0.min.js
jquery.mobile-1.5.0.min.js
/images (存放jQM需要的图标文件)
引入本地文件:
html复制<link rel="stylesheet" href="css/jquery.mobile-1.5.0.min.css">
<script src="js/jquery-3.6.0.min.js"></script>
<script src="js/jquery.mobile-1.5.0.min.js"></script>
对于使用构建工具的项目:
bash复制npm install jquery-mobile --save
然后在入口文件中引入:
javascript复制import 'jquery-mobile/dist/jquery.mobile.css'
import $ from 'jquery'
import 'jquery-mobile'
html复制<!DOCTYPE html>
<html>
<head>
<title>我的应用</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 引入jQuery Mobile -->
</head>
<body>
<div data-role="page" id="home">
<div data-role="header">
<h1>欢迎页</h1>
</div>
<div role="main" class="ui-content">
<p>这里是页面内容</p>
<a href="#about" class="ui-btn">关于我们</a>
</div>
<div data-role="footer">
<h4>页脚内容</h4>
</div>
</div>
</body>
</html>
在单个HTML文件中管理多个"页面"是jQuery Mobile的特色:
html复制<!-- 首页 -->
<div data-role="page" id="main">
<!-- 首页内容 -->
<a href="#settings">跳转到设置</a>
</div>
<!-- 设置页 -->
<div data-role="page" id="settings">
<!-- 设置页内容 -->
<a href="#main" data-direction="reverse">返回</a>
</div>
技巧:添加data-direction="reverse"可以让页面切换时有反向动画效果
jQuery Mobile提供5种主题(a-e),通过data-theme属性应用:
html复制<div data-role="page" data-theme="a"> <!-- 黑色主题 -->
<div data-role="header" data-theme="b"> <!-- 蓝色标题栏 -->
主题示例对照表:
| 主题 | 主色调 | 适用场景 |
|---|---|---|
| a | 黑色 | 夜间模式 |
| b | 蓝色 | 默认主题 |
| c | 灰色 | 中性背景 |
| d | 白色 | 亮色模式 |
| e | 黄色 | 高亮提醒 |
使用ThemeRoller在线工具生成主题:
https://themeroller.jquerymobile.com/
手动覆盖CSS变量(jQM 1.5+):
css复制:root {
--ui-btn-active-bg: #ff5722;
--ui-bar-border-color: #e91e63;
}
html复制<form>
<!-- 带图标的输入框 -->
<div class="ui-field-contain">
<label for="username">用户名:</label>
<input type="text" id="username" placeholder="输入用户名">
</div>
<!-- 滑动开关 -->
<select id="switch" data-role="slider">
<option value="off">关闭</option>
<option value="on">开启</option>
</select>
</form>
常见问题:iOS设备上表单元素样式异常时,添加meta标签:
<meta name="apple-mobile-web-app-capable" content="yes">
html复制<ul data-role="listview" data-inset="true">
<li data-icon="star"><a href="#">收藏项</a></li>
<li>
<a href="#">
<img src="avatar.png" class="ui-li-icon">
<h2>带图标的项目</h2>
<p>描述文字</p>
</a>
</li>
<li data-role="list-divider">分隔项</li>
</ul>
动态加载列表数据:
javascript复制$("#list").empty().listview("refresh");
$.each(data, function(i, item) {
$("#list").append(`<li><a href="#">${item.text}</a></li>`);
});
$("#list").listview("refresh");
只引入需要的模块:
javascript复制// 禁用自动初始化
$(document).on("mobileinit", function() {
$.mobile.autoInitializePage = false;
});
// 手动初始化特定组件
$.mobile.page.prototype.options.degradeInputs = {
color: false,
date: true
};
javascript复制// 禁用页面预加载
$.mobile.defaultPageTransition = "none";
$.mobile.loader.prototype.options.disabled = true;
// 自定义页面缓存
$(document).on("pagecontainerbeforeload", function(e, data) {
if(data.absUrl.indexOf("help.html") > -1) {
data.options.reloadPage = true;
}
});
症状:组件无法正常渲染
解决方案:
错误示例:
html复制<script src="custom.js"></script> <!-- 错误位置 -->
<script src="jquery.js"></script>
症状:通过AJAX加载的内容没有样式
解决方案:
javascript复制// 正确刷新组件的方法
$("#dynamic-content").trigger("create");
// 列表视图特殊处理
$("#listview").listview("refresh");
// 表单元素刷新
$("input[type='radio']").checkboxradio("refresh");
在jQuery Mobile中使用$.mobile.changePage跨域跳转时:
javascript复制$.mobile.pageContainer.one("pagecontainershow", function() {
window.location.href = "http://external.com";
});
javascript复制new Vue({
el: '#app',
mounted() {
$(document).on("pagecreate", function() {
// Vue和jQM协同工作
});
},
methods: {
refreshJQM() {
this.$nextTick(() => {
$("[data-role='page']").trigger("create");
});
}
}
});
css复制/* 断点覆盖 */
@media (min-width: 768px) {
.ui-page {
max-width: 768px;
margin: 0 auto;
}
}
code复制/contacts-app
/css
jquery.mobile-1.5.0.min.css
custom.css
/js
jquery-3.6.0.min.js
jquery.mobile-1.5.0.min.js
app.js
/images
icons/
index.html
details.html
javascript复制// 联系人列表页
$(document).on("pagecreate", "#contacts", function() {
$.getJSON("/api/contacts", function(data) {
let items = data.map(c => `
<li>
<a href="#detail?id=${c.id}">
<img src="images/${c.avatar}">
<h2>${c.name}</h2>
<p>${c.phone}</p>
</a>
</li>
`);
$("#contacts-list").html(items).listview("refresh");
});
});
// 详情页参数处理
$(document).on("pagebeforeshow", "#detail", function(e, data) {
const id = $.mobile.getUrlParams().id;
loadContactDetails(id);
});
从旧版本升级到1.5的注意事项:
移除已被废弃的API:
样式类名变更:
新特性适配:
javascript复制// 启用增强滚动
$.mobile.touchOverflowEnabled = true;
// 使用新的事件系统
$(document).on("pagecontainerbeforechange", function() {
// 导航拦截逻辑
});