jQuery .load方法无法异步加载到div图像

时间:2014-08-09 13:53:34

标签: jquery asynchronous load

我正在尝试加载来自我的" img"文件夹到div #scrollerContent但我无法让它正常工作。

//Variables References
        var sections = $(".nav a"),
            pageBody = $("#scrollerContent"),
            next = $('.next'),
            prev = $('.prev'),
            img = $('img');

        //Manage loading of pages
        sections.on('click', function(){
            pageBody.load("img/" + this.id + ".jpg");
            sections.removeClass('selected');
            $(this).addClass('selected');

2 个答案:

答案 0 :(得分:0)

对于遇到类似问题的人,以下代码适用于我。

//Variables References
    var sections = $(".nav a"),
        next = $('.next'),
        prev = $('.prev'),
        img = $('img'),
        largeImg = $('img.large');

    //Manage loading of pages
    sections.on('click', function(){
        largeImg.attr('src',"img/" + this.id + ".jpg"); 
        sections.removeClass('selected');
        $(this).addClass('selected');

答案 1 :(得分:0)

利用.load()

在服务器上,给定

html,

,即/img/images.html

<img id="image1" src="/img/path/to/image" />

document

<a href="#" class="image1">click</a>

JS

    var sections = $(".nav a"),
        pageBody = $("#scrollerContent"),
        next = $('.next'),
        prev = $('.prev'),
        img = $('img');

    //Manage loading of pages
    sections.on('click', function(e){
      e.preventDefault(); 
      pageBody.load("/img/images.html #" + $(e.target).attr("class"));
        sections.removeClass('selected');
        // is `$(this)` here intended for `<img>` , or `sections` ?
        $(this).addClass('selected');
    });

jsfiddle http://jsfiddle.net/guest271314/t1gkq3qc/