j查询在本地运行网站时未在 MAMP (1.11.2) 中加载

jQuery not loading in MAMP (1.11.2) when running website locally

本文关键字:加载 MAMP 查询 运行 网站      更新时间:2023-09-26

首先,我很抱歉这个问题之前已经被问过很多次了,但是没有一个答案解决了我的问题,所以我想我会在这里问这个问题。

我下载了jQuery 1.11.2(未压缩)来本地开发我的网站,但是使用内置于jsfiddle的jQuery的代码在本地不起作用。因此,我添加了以下代码来检查jQuery是否已加载:

<script src="file:///Applications/MAMP/htdocs/phpv1/v1/jquery-1.11.2.js">
if (typeof jQuery != 'undefined') {
alert("jQuery library is loaded!");
}else{
alert("jQuery library is not found!");
}
</script>
但是,

我没有收到任何警报框,让我怀疑是否是Javascript不起作用,但是我使用一些简单的表单验证代码让Javascript工作。我正在使用MAMP来托管PHP和Google Chrome作为浏览器。该文件另存为 HTML 文件,代码如下:

<html>    
<style>
 #test{
max-height: 150px;
max-width: 200px;
overflow: auto;
background: #50a8ff;    
 }
</style>    
<head>
<script src="file:///Applications/MAMP/htdocs/phpv1/v1/jquery-1.11.2.js">
if (typeof jQuery != 'undefined') {
alert("jQuery library is loaded!");
}else{
alert("jQuery library is not found!");
}
</script>    
</head>
<body>
</body>
</html>

我显然无法使用谷歌托管的图书馆,因为我在本地运行网站。为了确定Javascript的文件路径,我在Google Chrome中打开了它。

不应将内容放入具有src属性的 <script> 标记中。使用两个单独的标签进行测试:

<script src="file:///Applications/MAMP/htdocs/phpv1/v1/jquery-1.11.2.js"></script>
<script>
if (typeof jQuery != 'undefined') {
    alert("jQuery library is loaded!");
}else{
    alert("jQuery library is not found!");
}
</script>

你有两个选择:要么使用谷歌托管的jQuery

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

或者然后您需要更改为使用相对路径来文件,

如下所示
<script src="jquery-1.11.2.js"></script>

jquery-1.11.2.js 与 HTML 页面位于同一文件夹中时。

如果你从 MAMP 运行它,你需要改变这一行 -

<script src="file:///Applications/MAMP/htdocs/phpv1/v1/jquery-1.11.2.js">

对此——

<script src="phpv1/v1/jquery-1.11.2.js" type="text/javascript">

如果您的 HTML 文件在"htdocs"中。如果您不使用HTML5,我将type="text/javascript"包括在内。