javascript - 单击时加载已删除的 div 元素

标签 javascript jquery html

我有以下两个 div 元素:

<div class='staticMap'></div>
<div class='geolocation-common-map'></div>

加载页面时,使用 jQuery 删除名为 'geolocation-common-map' 的 div。我想知道在单击 div 'staticMap' 后如何再次加载此 div。到目前为止,我有以下 jQuery,但我需要替换 .show() 函数。

$('.geolocation-common-map').remove();
$('.staticMap').on("click", function(event) {
   $('.geolocation-common-map').show();
   $('.staticMap').remove();
   event.preventDefault();
});

有什么想法可以做到这一点吗?

提前谢谢您。

最佳答案

根据detach documentation :

The .detach() method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.

您可以按如下方式使用它:

var dv_geolocation_common_map = $( ".geolocation-common-map" ).detach();
var dv_staticMap = $('.staticMap');

$('.staticMap').on("click", function(event) {
  dv_geolocation_common_map.insertAfter(dv_staticMap)
   dv_staticMap.detach();
   event.preventDefault();
});
.staticMap{
width:100px;
height:100px;
background-color:green;
}
.geolocation-common-map{
width:100px;
height:100px;
background-color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='staticMap'></div>
<div class='geolocation-common-map'></div>

关于javascript - 单击时加载已删除的 div 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55163752/

相关文章:

javascript - 数据表:是否可以根据下拉列表中的选择动态更改要查看的数据表的列?

javascript - 以列表格式动态显示 JSON 值

jquery - 防止表单提交触发按钮

javascript - Materialise 获取模态触发元素

javascript - 按钮在 iPad 中不起作用

javascript - 验证 : Is it possible to add many rules from data on a text component with vuetify?

javascript - 按类名单击特定链接

php - 使用 PHP 和复选框更新 MySQL 表

javascript - 如何在边框中心对齐两个元素?

javascript - 鼠标悬停在 html 上更改图像