删除现有子节点后无法将子节点附加到 div

can not append a childNode to a div after removing existing childNodes

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

这里我检查一下div里面有没有超链接,如果有,我就全部删除。

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

            node.removeChild(node.lastChild);
        }

然后我创建了新的超链接,但它们没有显示在 div 中。 顺便说一下,我正在使用 jquery 移动面板。如果我不删除 div.

的子节点,下面的代码也可以正常工作并在 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();

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

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