简体   繁体   English

Ajax 替换而不是追加

[英]Ajax replace instead of append

I used the following jQuery example which works like a charm.我使用了下面的 jQuery 示例,它就像一个魅力。 However it appends the results.但是,它会附加结果。 What do I need to change to replace the results instead of appending?我需要更改什么来替换结果而不是追加?

you could empty the element before you append您可以在追加之前清空元素

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

or use the html method或者使用html方法

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

Just change只是改变

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

to

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

OK, last entry 2009, but if the problem still exist:好的,最后一个条目是 2009 年,但如果问题仍然存在:

let oldElement = $('#results')
let content = '<div id="#results>9000+</div>'
oldElement = oldElement.replaceWith(content)
let newElement = $('#results')

Empty function before your append or prepend function.附加或前置函数之前的空函数。

I tried this & it worked --->我试过了,它奏效了--->

frame is id selector & it append image.框架是 id 选择器 & 它附加图像。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM