Javascript 追加后,是否要删除

Javascript 追加后,是否要删除,javascript,jquery,append,Javascript,Jquery,Append,我的地图容器中添加了一些缩放按钮 我想在使用特定地图图像时删除这些缩放按钮 代码: JS: 在我以单独的方法添加缩放按钮后,我不确定“删除”缩放按钮的正确方式,因为我上面向您展示的方式不起作用(运行程序时按钮仍然存在)您可以在地图中创建这样的方法,并在您想要删除按钮时使用它 Map = function() { var $MapContainer; this.initMap = function($container, zoomHidden) { $MapContainer =

我的地图容器中添加了一些缩放按钮

我想在使用特定地图图像时删除这些缩放按钮

代码: JS:


在我以单独的方法添加缩放按钮后,我不确定“删除”缩放按钮的正确方式,因为我上面向您展示的方式不起作用(运行程序时按钮仍然存在)

您可以在地图中创建这样的方法,并在您想要删除按钮时使用它

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);
    }
  }
}
Map=function(){
var$MapContainer;
this.initMap=函数($container,zoomHidden){
$MapContainer=$container;
$MapContainer.append(“”);
如果(!zoomHidden){
$MapContainer.append(“+”);
$MapContainer.append(“-”);
}
}
this.removeZoomButtons=函数($container){
$container.remove(“#mapZoomIn”);
$container.remove(“#mapZoomOut”);
}
this.setMapProperties=函数(mapImage){
$('.MapImage').css('background-image','url('+MapImage+'));
//我要删除缩放按钮的位置
if($('.MapImage').css('background-image')==“url(../images/mapImages/noZooMap.jpg)”){
这是。移除ZoomButtons($MapContainer);
}
}
}
这行是:

$('.MapImage').css('background-image','url("mapImage")');
你的代码里到底有什么?如果是这样,它将在文本URL“mapImage”而不是
mapImage
变量中指定的位置查找图像


将其更改为
$('.MapImage').css('background-image','url('+MapImage+'))应该可以做到这一点。

哎呀,我在真正的代码中实际上是这样的,但我简化了代码以保护公司代码,在编辑中我把这部分放错了。:)我将在if语句
$MapContainer.remove('.MapZoomInButton')中编辑示例代码,但不会这样说;$MapContainer.remove('.MapZoomOutButton')也可以简单地工作?甚至
$MapContainer.remove(“#mapZoomIn”);$MapContainer.remove(“#mapZoomOut”)?可能会,但现在您的设计更灵活了。您已经创建了一个可以在Map对象中以及Map对象外部重用的方法。我明白了!我尝试了我在这里评论的两种方法,以及示例中给出的方法,但仍然不起作用。这肯定是我的问题,但你确实回答了我的基本问题,即如何正确地移除这些财产。谢谢查看if语句。如果($('.MapImage').css('background-image')==“url(../images/mapImages/noZooMap.jpg)”),请验证代码
中的以下内容是否正确。
。。可能会在if语句之前提醒出
$('.MapImage').css('background-image')
的值。我们应该确保条件是你所期望的。
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);
    }
  }
}
$('.MapImage').css('background-image','url("mapImage")');