当页面没有通过localhost/ (xampp服务器)打开时,Jquery加载不起作用

Jquery load doesn't work when page is not opened through localhost/ (xampp server)

本文关键字:服务器 不起作用 加载 Jquery xampp localhost      更新时间:2023-09-26

任何人都可以帮助我解释为什么当我打开相同的文件(一个我把它放在桌面,另一个我把它放在xampp内的htdocs文件夹)完全相同的文件。当我在桌面上打开文件时,jquery load()不起作用,但是当我在xampp文件夹中打开文件时,jquery load()工作正常。

我理解在php文件的情况下,页面需要在xampp文件夹内工作,但这些只是html文件,jquery加载需要使用服务器端脚本吗?请帮我解释一下。

html文件:
<!DOCTYPE HTML>
<html>
<head>
<script src='./js/jquery.js'></script>
<script>
$(document).ready(function() {
    $(".div").load("test.txt");
});
</script>
</head>
<body>
    <div class='div'></div>
</body>
</html>

用法:

test

文件夹结构:

test
  >> js
     >> jquery.js
  >> test.html
  >> test.txt

因为你包含JQuery相对于你正在加载的文档:

<script src='./js/jquery.js'></script>

如果你把它改成:

<script src='//code.jquery.com/jquery-1.11.3.min.js'></script>

它应该在任何地方工作,因为你是从远程CDN加载jQuery,而不是本地。