jQuery .load()在onclick上不起作用 [英] jquery .load() not working onclick
问题描述
我试图建立一个网站,以便在页面加载时有一个div,它可以从我已设置的.html页面中动态提取其内容.我在顶部有一堆缩略图,当您单击一个缩略图时,我希望来自其他.html文档的内容替换该div中首次动态加载的内容.
I'm trying to have a site set up so when the page loads there is a div that dynamically pulls it's content from a .html page I have set up. I have a bunch of thumbnails at the top and when you click one I want the content from a different .html document to replace what every was in that div that was dynamically loaded into the first time.
为此,我尝试使用jquery .load()功能.我试图做的是设置一个函数:
To do this I'm trying to use the jquery .load() feature. What I tried to do was set up a function:
<script type="text/javascript">
$(document).ready(function space_load() {
$('#SPACE_TOTAL').load('http://www.klossal.com/portfolio/space.html');
}
</script>
,然后尝试使用以下命令启动它:
and then tried to launch it using:
onclick="space_load();"
这没有用,我想知道是否有人可以帮助我.我想知道的另一件事是,如果这行得通,它会代替以前加载到那里的内容吗?我可能会变得头脑清醒,这只能靠它自己完成.
this didn't work and I was wondering if anyone could help me with this. The other thing I was wondering is if this were to work, would it replace the content that was previously loaded into there? I might be getting a head of myself and it just does this on it's own.
推荐答案
首先,您的代码结构无效
First, your code has invalid structure
$(document).ready(function() {
function space_load() {
$('#SPACE_TOTAL').load('http://www.klossal.com/portfolio/space.html');
}
space_load(); //Now call the function
});
但是,您可以将其修整
$(function() {
$('#SPACE_TOTAL').load('http://www.klossal.com/portfolio/space.html');
});
但是,由于您希望在单击元素时执行此操作. 这是您需要的:
But, since you want this on click of an element. This is what you need:
$(function() {
$("#yourlinkid").click(function() {
$('#SPACE_TOTAL')
.html('<img src="preloader.gif" />')
.load('http://www.klossal.com/portfolio/space.html');
});
});
这篇关于jQuery .load()在onclick上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!