2013-04-01 11 views
0

這是我的代碼:更換有兩個可能的ID的容器

function changeimage2() 
{ 
    /*$("#container1").css({'visibility':'hidden'});*/ 

    $("#PicInRightTitle").css({'visibility':'hidden'}); 

    $("Img").click(function(){ 
     $(document).ready(function(){ 
      $("#container1").replaceWith($('#container2')); 
      $("#container2").show(); 
      $("#slider").easySlider({ 
       auto: false, 
       continuous: true 
      }); 
     }); 
    }); 
} 

上面的代碼替換container2的。它container1工作。 但我想替換容器1或容器2與容器3的div 我試過這個,但它不起作用。

($("#container1") || $("#container3")).replaceWith($('#container2')); 

我該如何正確地做到這一點?

回答

1

這將取代#container1如果在頁面上,否則將取代#container2

if ($("#container1").length > 0) 
    $("#container1").replaceWith($('#container3')); 
else 
    $("#container2").replaceWith($('#container3')); 
+0

我不太清楚我們如何使用這個。爲什麼是需要的長度? –

0

可以使用普通類爲那些如下:

$('.containers').replaceWith($('#container2'));

+0

這是一個偉大的建議!非常感謝!! –

+0

@priyanka:如果這個答案對您有幫助,那麼您應該將其標記爲已接受並且可能贊成。 – Vishal

-1

試試這個:

$("#container1,#container3").replaceWith($('#container2')); 
+0

感謝您的快速回復!似乎工作!但我的代碼有點複雜,即時嘗試正確實現。但無論如何,謝謝! –

相關問題