2011-01-11 147 views
2

有人可以幫我嗎?在jQuery的其餘部分構建元素列表之前,我需要隱藏.jshowoff-link元素。然後我在最後顯示相同的元素。jQuery .show();函數不顯示以前隱藏的所有元素

它生成鏈接中的圖像列表。出於某種原因,雖然它只顯示第一個圖像和鏈接,而不是其他的。

我試過交換.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; 
    }); 
+0

我建議使用[`活()`](HTTP://api.jquery。 com/live /),但我不確定可以使用哪種事件類型...... – 2011-01-11 10:53:13

回答

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

第二種解決方案完美運行。非常感激。 – 2011-01-11 13:22:57