Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

jquery .load() not working onclick

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
});

However, you can trim it down do this

$(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');
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...