jQuery Ajax 替换而不是追加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1675215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:57:23  来源:igfitidea点击:

Ajax replace instead of append

jqueryajaxreplaceappend

提问by shaiss

I used the following jQuery examplewhich works like a charm. However it appends the results. What do I need to change to replace the results instead of appending?

我使用了以下 jQuery示例,它的作用就像一个魅力。但是它附加了结果。我需要更改什么来替换结果而不是附加?

回答by Marek Karbarz

you could empty the element before you append

你可以在追加之前清空元素

$("#results").empty().append(myHtml);

or use the htmlmethod

或使用html方法

$("#results").html(myHtml)

回答by Greg

Just change

只是改变

$('#results').append(myHtml);

to

$('#results').html(myHtml);

回答by zkaptic

ok, last entry 2009, but if the problem still exist:
$("#results").replaceWith(yourContent)

好的,最后一次进入 2009 年,但如果问题仍然存在:
$("#results").replaceWith(yourContent)

回答by Namesh Silva

Empty function before your append or prepend function.

在 append 或 prepend 函数之前清空函数。

I tried this & it worked --->

我试过了,它奏效了--->

frame is id selector & it append image.

frame 是 id 选择器 & 它附加图像。

$("#frame").empty().append('<img src="" >' );