.load jQuery函数在使用特定div时不起作用 [英] .load jquery function not working when using specific div

查看:78
本文介绍了.load jQuery函数在使用特定div时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此行不起作用

$('#bookcontainer').load( startSlide );

这行是

$(window).load( startSlide );

#bookcontainer是包含四个图像的div.

#bookcontainer is a div containing four images.

这是我的完整代码:

// JavaScript Document
$('#bookcontainer').load( startSlide );

var slide;

$('#slider').hover( 
function() {
        $('.arrow').show();
}, 
function() {
        $('.arrow').hide();
});

function startSlide() {
    $('.book').show();
    slide = setInterval(slideR, 5000);   
}


function slideR() {
    $('.book').first().css('left', '960px').appendTo('#bookcontainer').animate(
    {
        "left": "-=960px"
    }, {
        duration: 1000,
        easing: 'easeOutCubic'
    });

}


function slideL() {
    $('.book').last().animate(
    { "left":"+=960px" }, {
        duration: 1000,
        easing: 'easeOutCubic',
        complete: function() {
        $(this).prependTo('#bookcontainer').css('left', '0px');   
        }
    });

}


function right() {
    clearInterval(slide);
    slideR();
    startSlide();
};


function left() {
    clearInterval(slide);
    slideL();
    startSlide();
};

推荐答案

只能在与网址相关联的元素上处理load事件.

The load event can only be handled on elements that are associated with a url. From the documentation:

load事件发送到一个元素 当它和所有子元素都被 完全加载.此事件可以是 发送到与 网址:图片,脚本,框架,iframe, 和窗口对象.

The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.

如果您正在等待所有图像加载,则可以将事件处理程序分别应用于这些图像.

If you're waiting on all of your images to load, you could apply the event handler to those images individually.

此外:$(window).load(...)在页面上的所有元素(包括图形)完全加载之前不会被触发,因此不必将事件绑定到div以确保已加载子元素.

Also: $(window).load(...) will not be fired until all elements on the page are fully loaded (including graphics), so it's not necessary to bind the event to a div to make sure child elements have loaded.

如果您要发出AJAX请求并尝试检测何时已将内容加载到该div中,则应使startSlide成功回调到您的AJAX请求中.

If you're making an AJAX request and attempting to detect when content has been loaded into that div, you should make startSlide the success callback to your AJAX request.

这篇关于.load jQuery函数在使用特定div时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆