CDN(Content Delivery Network,内容分发网络)是一种分布式网络服务,通过将内容缓存到离用户最近的服务器上,提高访问速度和可靠性。
使用 CDN 引入 jQuery 的优势:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
特点:
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
特点:
<script src="https://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.2.min.js"></script>
特点:
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
特点:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
特点:
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.6.0.min.js"></script>
特点:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
特点:
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
特点:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery CDN 引入示例</title>
<style>
.container {
max-width: 900px;
margin: 0 auto;
padding: 20px;
font-family: Arial, sans-serif;
}
.cdn-section {
margin-bottom: 30px;
padding: 20px;
background-color: #f8f9fa;
border-radius: 8px;
}
.cdn-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin-top: 15px;
}
.cdn-card {
padding: 15px;
background-color: white;
border-radius: 8px;
border: 2px solid #ddd;
cursor: pointer;
transition: all 0.3s;
}
.cdn-card:hover {
border-color: #3498db;
transform: translateY(-3px);
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.cdn-card h4 {
margin: 0 0 10px 0;
color: #2c3e50;
}
.cdn-card p {
margin: 5px 0;
font-size: 12px;
color: #7f8c8d;
}
.test-area {
margin-top: 20px;
padding: 20px;
background-color: white;
border-radius: 8px;
border: 2px dashed #3498db;
}
.status-box {
margin-top: 15px;
padding: 15px;
border-radius: 5px;
display: none;
}
.status-box.success {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status-box.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.status-box.loading {
background-color: #fff3cd;
color: #856404;
border: 1px solid #ffeaa7;
}
</style>
</head>
<body>
<div class="container">
<h1>jQuery CDN 引入测试</h1>
<div class="cdn-section">
<h3>选择 CDN 服务商</h3>
<p>点击下方卡片测试 CDN 连接:</p>
<div class="cdn-grid">
<div class="cdn-card" onclick="testCDN('https://code.jquery.com/jquery-3.6.0.min.js', '官方 CDN')">
<h4>官方 CDN</h4>
<p>code.jquery.com</p>
<p>版本最新,全球节点</p>
</div>
<div class="cdn-card" onclick="testCDN('https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js', '百度 CDN')">
<h4>百度 CDN</h4>
<p>apps.bdimg.com</p>
<p>国内访问快速</p>
</div>
<div class="cdn-card" onclick="testCDN('https://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.2.min.js', '又拍云 CDN')">
<h4>又拍云 CDN</h4>
<p>upcdn.b0.upaiyun.com</p>
<p>免费 CDN 服务</p>
</div>
<div class="cdn-card" onclick="testCDN('https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js', '新浪 CDN')">
<h4>新浪 CDN</h4>
<p>lib.sinaapp.com</p>
<p>新浪提供服务</p>
</div>
<div class="cdn-card" onclick="testCDN('https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js', 'Google CDN')">
<h4>Google CDN</h4>
<p>ajax.googleapis.com</p>
<p>全球 CDN 节点</p>
</div>
<div class="cdn-card" onclick="testCDN('https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.6.0.min.js', 'Microsoft CDN')">
<h4>Microsoft CDN</h4>
<p>ajax.aspnetcdn.com</p>
<p>微软提供服务</p>
</div>
<div class="cdn-card" onclick="testCDN('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js', 'cdnjs CDN')">
<h4>cdnjs CDN</h4>
<p>cdnjs.cloudflare.com</p>
<p>开源库 CDN</p>
</div>
<div class="cdn-card" onclick="testCDN('https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js', 'jsDelivr CDN')">
<h4>jsDelivr CDN</h4>
<p>cdn.jsdelivr.net</p>
<p>支持 NPM 包</p>
</div>
</div>
</div>
<div class="cdn-section">
<h3>CDN 测试结果</h3>
<div class="test-area">
<div id="cdnStatus" class="status-box"></div>
<div id="versionInfo" style="margin-top: 15px; padding: 15px; background-color: #e8f5e9; border-radius: 5px; display: none;"></div>
</div>
</div>
<div class="cdn-section">
<h3>jQuery 功能测试</h3>
<button onclick="testFunction()" style="padding: 10px 20px; background-color: #3498db; color: white; border: none; border-radius: 5px; cursor: pointer;">测试 jQuery 功能</button>
<div id="functionTest" style="margin-top: 15px; padding: 15px; background-color: #e8f5e9; border-radius: 5px; display: none;"></div>
</div>
</div>
<script>
function testCDN(url, name) {
$("#cdnStatus").removeClass("success error loading").addClass("loading").show().html(`
<p><strong>正在测试 ${name}...</strong></p>
<p>URL: ${url}</p>
`);
$("#versionInfo").hide();
$.getScript(url)
.done(function() {
$("#cdnStatus").removeClass("loading error").addClass("success").html(`
<p><strong>✓ ${name} 连接成功!</strong></p>
<p>URL: ${url}</p>
`);
$("#versionInfo").show().html(`
<p><strong>jQuery 版本:</strong>${$.fn.jquery}</p>
<p><strong>浏览器:</strong>${navigator.userAgent}</p>
`);
})
.fail(function() {
$("#cdnStatus").removeClass("loading success").addClass("error").html(`
<p><strong>✗ ${name} 连接失败</strong></p>
<p>URL: ${url}</p>
<p>请检查网络连接或尝试其他 CDN</p>
`);
$("#versionInfo").hide();
});
}
function testFunction() {
if (typeof $ === 'undefined') {
alert("请先选择并测试一个 CDN");
return;
}
$("#functionTest").show().html(`
<p><strong>✓ jQuery 功能正常</strong></p>
<p>版本:${$.fn.jquery}</p>
<p>选择器测试:${$("h1").length} 个 h1 元素</p>
<p>当前时间:${new Date().toLocaleString()}</p>
`);
}
</script>
</body>
</html>
对于国内项目,优先选择以下 CDN:
对于国际项目,推荐以下 CDN:
为了确保高可用性,可以使用多个 CDN 作为备份:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
if (typeof jQuery === 'undefined') {
document.write('<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"><\/script>');
}
</script>
| 特性 | CDN | 本地文件 |
|---|---|---|
| 加载速度 | 快(就近节点) | 慢(需下载) |
| 网络依赖 | 需要 | 不需要 |
| 缓存优势 | 自动缓存 | 需手动配置 |
| 离线使用 | 不支持 | 支持 |
| 版本管理 | 自动更新 | 手动管理 |
| 成本 | 免费 | 占用服务器空间 |
使用 CDN 引入 jQuery 是推荐的方式,特别是对于在线项目。选择 CDN 时应考虑:
对于国内项目,推荐使用百度 CDN 或又拍云 CDN;对于国际项目,推荐使用官方 CDN 或 cdnjs CDN。