问题描述
我是JavaScript世界的新手,我正在尝试使用功能load()插入另一个html文件.有点难以解释,这是代码:
I am new to the world of JavaScript and I'm trying to use the function load() to insert another html file. It' a little bit hard to explain, here is the code:
<script>
$(document).ready(function() {
$('#main').click(
function(){
$('#news').load('today.html');
}
); //end click
}); //end ready
</script>
你能帮我吗?我没有使用网络服务器.谢谢
Can you help me? I'm not using a web server.Thanks
推荐答案
我不认为您提供的代码有任何错误.我相信这与您加载JQuery库有关,因为通过以下代码我获得了这些结果:
I do not believe the code you have presented has any faults in any way. I believe it is to do with your loading of the JQuery library, as with the following code I achieved these results:
index.html
index.html
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$('#main').click(
function(){
$('#news').load('news.html');
}
); //end click
}); //end ready
</script>
</head>
<body>
<p id="main">HELLO</p>
<p id="news">NEWS</p>
</body>
</html>
news.html
news.html
<html>
<head></head>
<body><h1>HELLO STACK OVERFLOW!!!</h1></body>
</html>
点击前:
点击后:
但是,在构建此示例时,我首先尝试使用 Google API 版本的JQuery,但发现当前无法连接到该API.因此,我认为解决您的问题的方法是转到以下网站: http://code .jquery.com/jquery-1.11.1.js 并将所有内容复制并粘贴到名为"jquery.js"的文本文件中.然后将以下内容添加到您的主HTML文件的head标签中:
However, when I was building this example I first tried using the Google APIs version of JQuery and found that I could not currently connect to the API. Therefore I believe the solution to your problem is to go to this website: http://code.jquery.com/jquery-1.11.1.js and copy and paste everything into a text file called 'jquery.js'. Then add the following to the head tag of your main HTML file:
<script src="jquery.js"></script>
请确保"jquery.js"与项目的主HTML文件位于同一目录中,否则将无法正常工作.希望这会有所帮助:)
Make sure that 'jquery.js' is in the same directory as the main HTML file of your project otherwise this will not work. Hope this helps :)
这篇关于jQuery load()函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!