2011-09-22 94 views
0

我有些變焦按鈕添加到我的地圖容器追加後,要刪除

我想刪除這些變焦按鈕被用於特定的地圖圖像時

代碼: JS:

Map = function() 
{ 

//private: 
var $MapContainer; 

this.initMap = function($container, zoomHidden) 
{ 
    $MapContainer = $container; 
    $MapContainer.append("<div class='MapImage'></div>"); 

    if(!zoomHidden) 
    { 
     $MapContainer.append("<div id='mapZoomIn' class='MapZoomInButton'>+</div>"); 
     $MapContainer.append("<div id='mapZoomOut' class='MapZoomOutButton'>-</div>"); 
    } 
} 

this.setMapProperties = function(mapImage) 
{ 

    $('.MapImage').css('background-image','url("'+mapImage+'")'); 

    // Where I want to remove the zoom buttons 
    if($('.MapImage').css('background-image') === "../images/mapImages/noZooMap.jpg") 
    { 
     $('.MapZoomInButton').remove(); 
     $('.MapZoomOutButton').remove(); 
    } 
} 
} 

CSS:

.MapImage 
{ 
position:absolute; 
height:100%; 
width:100%; 
top:0; 
right:0; 
background-image: url("../images/mapImages/GenericMapImage.jpg"); 
background-repeat:no-repeat; 
} 

我不知道正確的方法在我將它們放在一個單獨的方法中後,將其「刪除」縮放按鈕,因爲上面顯示的方式不起作用(運行程序時按鈕仍然存在)

回答

1

您可以在Map中創建一個方法像這樣,當你想要刪除按鈕時使用它。

Map = function() { 
    var $MapContainer; 

    this.initMap = function($container, zoomHidden) { 
    $MapContainer = $container; 
    $MapContainer.append("<div class='MapImage'></div>"); 
    if(!zoomHidden) { 
     $MapContainer.append("<div id='mapZoomIn' class='MapZoomInButton'>+</div>"); 
     $MapContainer.append("<div id='mapZoomOut' class='MapZoomOutButton'>-</div>"); 
    } 
    } 

    this.removeZoomButtons = function ($container) { 
    $container.remove("#mapZoomIn"); 
    $container.remove("#mapZoomOut"); 
    } 

    this.setMapProperties = function(mapImage) { 
    $('.MapImage').css('background-image','url('+mapImage+')'); 
    // Where I want to remove the zoom buttons 
    if($('.MapImage').css('background-image') === "url(../images/mapImages/noZooMap.jpg)") { 
     this.removeZoomButtons($MapContainer); 
    } 
    } 
} 
+0

在if語句中不會說'$ MapContainer.remove('。MapZoomInButton'); \t \t \t $ MapContainer.remove('。MapZoomOutButton');'簡單地工作呢? – Briz

+0

甚至是$ MapContainer.remove('#mapZoomIn'); \t \t \t $ MapContainer.remove('#mapZoomOut');'? – Briz

+0

它會但現在你的設計更靈活。您已經創建了一個可以在Map對象以及地圖對象之外重用的方法。 –

0

這是行:

$('.MapImage').css('background-image','url("mapImage")'); 

正是你在你的代碼?如果是這樣,它將在文字URL「mapImage」中查找圖像,而不是在mapImage變量中指定的位置。

改變它爲$('.MapImage').css('background-image','url('+mapImage+')');應該做的伎倆。

+0

哎呀,我真的喜歡它在真實的代碼中,但我已經簡化了代碼以保護公司代碼,並且在我的編輯中,我將這個部分弄錯了。 :)我將編輯示例代碼 – Briz