你如何使用 cdn 来托管 jQuery?

How do you use a cdn to host jQuery?

我一直都是用jQuery文件来使用jquery。最近我听说使用 CDN。我想问一下你是怎么使用的,这行代码是做什么的:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

您是指任何 CDN 还是自己托管? Google 有 jquery 的 CDN(可在此处找到 https://developers.google.com/speed/libraries

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

内容分发网络 CDN 指的是一组地理分布的服务器,它们协同工作以提供 Internet 内容的快速分发。

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").hide();
  });
});
</script>
</head>
<body>

<h2>This is a heading</h2>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

<button>Click me</button>

</body>
</html>

这可能对您有帮助:https://code.jquery.com/

Select 你想要的 jQuery 版本(我更喜欢 jQuery3.x 缩小版)。

当你 select jQuery 版本(也有选项)时,你可以复制像

这样的脚本
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

在 html 中插入脚本 <head>:

...
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>
<body>
</body>
</html>
...