首页 > 解决方案 > 每次追加然后更改

问题描述

.append每次单击按钮时,我都在使用。

 $( ".kop_filters" ).append( "<p class='gekozen_datum'>Gekozen datum: </p>" );

如果我单击该按钮 4 次,这将附加在我的页面中,例如:

Gekozen datum:
Gekozen datum:
Gekozen datum:
Gekozen datum:

我希望它出现一次,每次单击它都会删除并再次附加,因此例如在单击它几次后,我将其打印一次。这是因为我想在其中加载一个每次点击都会改变的变量。

编辑:在这里找到解决方案:Ajax 替换而不是追加

标签: jqueryappend

解决方案


使用html方法。

$('button').click(() => {
  $(".kop_filters").html((Math.random() * 10).toString())
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="kop_filters"></div>
<button>Update</button>


推荐阅读