在dom中(由浏览器实现,innerhtml直到html5才是标准的,但由所有人实现),设置元素的innerhtml将删除它的所有内容。innerHTML和outerHTML的区别在于,outerHTML包含了标记本身。因此,outerHTML可用于替换标记。由于内容的类型是element,因此无论如何都不希望使用innerHTML,因此需要将其附加到所涉及的DOM元素。
这可能有效:
function SwapPlans(city, id) {
var tp = GetTravelPlanById(id);
var content = MakeHTMLAccordionMe(tp.items[0]);
var city = document.getElementById(city);
// Remove all children of the div
while (city.hasChildNodes()) {
city.removeChild(city.children.item(0));
}
// Add a child to the city, making sure that it belongs to this document. Instead
// of blind importing you might also check it's ownership.
city.appendChild(city.ownerDocument.importNode(content))