【问题标题】:can not append a childNode to a div after removing existing childNodes删除现有子节点后无法将子节点附加到 div
【发布时间】:2016-02-17 20:54:42
【问题描述】:

我的问题是在删除现有超链接后,我无法将新的超链接附加到 div 中。

这里我检查 div 内是否有任何超链接,如果有,我将其全部删除。

var node = document.getElementById('nearByCitiesDiv');
        while (node.hasChildNodes()) {

            node.removeChild(node.lastChild);
        }

然后我创建了新的超链接,但它们不会显示在 div 中。 顺便说一句,我正在使用 jquery 移动面板。如果我不删除 div 的 childNodes,下面的代码也可以正常工作并在 div 内添加新的超链接。

var element = $('<a data-role="button" style="text-decoration:none;"  href="#/" onclick="showNearCityWeather(' + nearbyPosition.lat + ',' + nearbyPosition.lng + ')" data-theme="a">' + c.toponymName + '</a >');
 $("#nearByCitiesDiv").controlgroup("container")["append"](element);
$("#nearByCitiesDiv").controlgroup("refresh");
$('[data-role="button"]').button();

【问题讨论】:

    标签: jquery-mobile appendchild removechild


    【解决方案1】:

    controlgroup 容器实际上是一个子 div。因此,当您删除子节点时,您实际上是在删除容器。您可以像这样清除容器:

    $("#nearByCitiesDiv").controlgroup("container").empty()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 2015-01-09
      • 1970-01-01
      • 2014-12-15
      相关资源
      最近更新 更多