jQuery .show();功能不显示以前隐藏的所有元素

时间:2011-01-11 09:34:07

标签: jquery jquery-plugins jquery-selectors

是的,有人能帮帮我吗?我需要在jQuery的其余部分构建元素列表之前隐藏.jshowoff-link元素。然后我在最后显示相同的元素。

它会在链接中生成图像列表。出于某种原因,虽然它只显示第一个图像和链接而不显示其他图像。

我已尝试交换.show();函数的位置并将其添加到最后if else语句中,但这也不起作用。

这样做是为了在.jshowoff();函数触发之前停止显示图像和链接列表。

所以我完全没有想法。有人可以帮忙吗?

// hide banners to begin with
$ ('.jshowoff-link').hide();

// this function wraps the elements in the neccessary tags to work with anything Slider
$ (document).ready(function() {
    $('a.jshowoff-link')
        .wrap('<li class="jshowoff-slide"></li>');
    $('li.jshowoff-slide')
        .wrapAll('<ul id="jshowoff"></ul>');
    // figures out if there's more than one <li> being produced
    if (banners.length > 1) {
        // if so, build the jshowoff
        $('#jshowoff').jshowoff({speed:7000, changeSpeed:1000, autoPlay:true, controls:true, links:true, animatePause:false, hoverPause:false });
        }
    else {
        // if not, disable the function
        $('#jshowoff').jshowoff({speed:7000, changeSpeed:1000, autoPlay:false, controls:false, links:false, animatePause:false, hoverPause:false });
        }
    // show the jshowoff after it's been built to stop flash of content on slow internet connections
    $('.jshowoff-link').show();
    return false;
    }); 

1 个答案:

答案 0 :(得分:1)

问题在于:当jshowoff启动时,它会从#jshowoff容器中删除所有子元素并将它们放入变量中。然后将它们逐个添加到容器中。因此,在您尝试show()时,您的链接不在DOM中。您可以做的是在调用jshowoff之前隐藏完整的jshowoff()元素,然后在调用结束后再次显示它:

$ (document).ready(function() {
    $('a.jshowoff-link')
        .wrap('<li class="jshowoff-slide"></li>');
    $('li.jshowoff-slide')
        .wrapAll('<ul id="jshowoff"></ul>');

    var $container = $('#jshowoff');

    $container.hide();

    // figures out if there's more than one <li> being produced
    if (banners.length > 1) {
        // if so, build the jshowoff
        $container.jshowoff({speed:7000, changeSpeed:1000, autoPlay:true, controls:true, links:true, animatePause:false, hoverPause:false });
        }
    else {
        // if not, disable the function
        $container.jshowoff({speed:7000, changeSpeed:1000, autoPlay:false, controls:false, links:false, animatePause:false, hoverPause:false });
        }
    // show the jshowoff after it's been built to stop flash of content on slow internet connections
    $container.show();

    return false;
}); 

如果你仍然有闪烁的元素,你也可以先隐藏链接,创建容器,隐藏它,再次显示链接,然后添加jshowoff并再次显示容器,如下所示:

// hide banners to begin with
$ ('.jshowoff-link').hide();

$ (document).ready(function() {
    $('a.jshowoff-link')
        .wrap('<li class="jshowoff-slide"></li>');
    $('li.jshowoff-slide')
        .wrapAll('<ul id="jshowoff"></ul>');

    var $container = $('#jshowoff');

    $container.hide();

    // The links are still attached to the DOM at this point, but hidden inside the hidden container.
    $('.jshowoff-link').show();

    // figures out if there's more than one <li> being produced
    if (banners.length > 1) {
        // if so, build the jshowoff
        $container.jshowoff({speed:7000, changeSpeed:1000, autoPlay:true, controls:true, links:true, animatePause:false, hoverPause:false });
        }
    else {
        // if not, disable the function
        $container.jshowoff({speed:7000, changeSpeed:1000, autoPlay:false, controls:false, links:false, animatePause:false, hoverPause:false });
        }
    // show the jshowoff after it's been built to stop flash of content on slow internet connections
    $container.show();

    return false;
});