jquery .append() 方法产生不需要的结果

jquery .append() method producing unwanted result

我正在使用维基百科的 API 进行一个项目。我已经能够成功加载 API 的数据。 但是,这是我尝试在提交输入值时将 API 的结果加载到 DOM 中的问题,但每次我尝试提交新值时,结果都会附加到第一次输入搜索的初始结果。

这里有一个 fiddle 可以表达我的意思:

https://jsfiddle.net/aonz929f/1/

    for( var key in data.query.pages){
        //loop through the JSON data 
       }

$("#result").append(htmlContent)/// Then I used the append method to return each returned result to the DOM

您必须先清除已经存在的 html,然后再添加新的,如下所示:

$("#result").html("")
for( var key in data.query.pages){
   $("#result").append(("<p>" + data.query.pages[key].title) + "</p>")
   //etc..        
}