jQuery Mobile 安装指南
jQuery Mobile 是一个基于 jQuery 的框架,用于构建响应式移动网站和应用程序。以下是详细的安装方法和代码示例。
通过 CDN 引入 jQuery Mobile
最简单的方式是通过 CDN 引入 jQuery Mobile。确保在引入 jQuery Mobile 之前先引入 jQuery。
<!DOCTYPE html>
<html>
<head>
<title>jQuery Mobile 示例</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!– 引入 jQuery –>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!– 引入 jQuery Mobile CSS –>
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<!– 引入 jQuery Mobile JS –>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>欢迎使用 jQuery Mobile</h1>
</div>
<div data-role="content">
<p>这是一个简单的 jQuery Mobile 页面。</p>
</div>
<div data-role="footer">
<h4>页脚内容</h4>
</div>
</div>
</body>
</html>
下载本地文件引入
如果需要离线使用,可以下载 jQuery Mobile 的文件并本地引入。
<!DOCTYPE html>
<html>
<head>
<title>jQuery Mobile 本地示例</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!– 引入本地 jQuery –>
<script src="js/jquery-3.6.0.min.js"></script>
<!– 引入本地 jQuery Mobile CSS –>
<link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css">
<!– 引入本地 jQuery Mobile JS –>
<script src="js/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>本地 jQuery Mobile</h1>
</div>
<div data-role="content">
<p>这是一个本地引入的 jQuery Mobile 页面。</p>
</div>
<div data-role="footer">
<h4>页脚内容</h4>
</div>
</div>
</body>
</html>
使用 npm 安装
对于使用 Node.js 的项目,可以通过 npm 安装 jQuery Mobile。
npm install jquery-mobile
安装完成后,在项目中引入:
import 'jquery-mobile';
基本页面结构示例
jQuery Mobile 使用 data-role 属性定义页面结构。以下是一个完整的页面示例:
<!DOCTYPE html>
<html>
<head>
<title>jQuery Mobile 页面结构</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<!– 主页面 –>
<div data-role="page" id="home">
<div data-role="header">
<h1>主页</h1>
</div>
<div data-role="content">
<p>欢迎来到主页。</p>
<a href="#about" data-role="button">关于我们</a>
</div>
<div data-role="footer">
<h4>版权信息</h4>
</div>
</div>
<!– 关于页面 –>
<div data-role="page" id="about">
<div data-role="header">
<h1>关于我们</h1>
<a href="#home" data-role="button" data-icon="home">返回</a>
</div>
<div data-role="content">
<p>这是关于我们的页面。</p>
</div>
<div data-role="footer">
<h4>版权信息</h4>
</div>
</div>
</body>
</html>
动态加载内容示例
jQuery Mobile 支持动态加载内容。以下是一个动态加载列表的示例:
<!DOCTYPE html>
<html>
<head>
<title>动态加载示例</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page" id="dynamic">
<div data-role="header">
<h1>动态加载</h1>
</div>
<div data-role="content">
<ul data-role="listview" id="list">
<!– 列表内容将通过 JavaScript 动态加载 –>
</ul>
</div>
</div>
<script>
$(document).on("pagecreate", "#dynamic", function() {
var items = ["项目 1", "项目 2", "项目 3", "项目 4"];
$.each(items, function(index, value) {
$("#list").append("<li><a href='#'>" + value + "</a></li>");
});
$("#list").listview("refresh");
});
</script>
</body>
</html>
主题定制示例
jQuery Mobile 提供多种主题样式,可以通过 data-theme 属性切换。
<!DOCTYPE html>
<html>
<head>
<title>主题定制示例</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>
<div data-role="page" id="themes">
<div data-role="header" data-theme="b">
<h1>主题示例</h1>
</div>
<div data-role="content">
<a href="#" data-role="button" data-theme="a">主题 A</a>
<a href="#" data-role="button" data-theme="b">主题 B</a>
<a href="#" data-role="button" data-theme="c">主题 C</a>
</div>
</div>
</body>
</html>
以上是 jQuery Mobile 的安装方法和基本使用示例,涵盖了 CDN 引入、本地文件引入、npm 安装以及常见功能实现。






