The reason is that you add new click listeners before adding new elements.(原因是您在添加新元素之前添加了新的Click侦听器。)
You can use the three-argument form of the click handler it will apply also to new elements added.(您可以使用点击处理程序的三参数形式,它也将应用于添加的新元素。)
$(document.body).on('click', '#add', function(){
$(this).after("<p> " + $("#newP").val() + "</p>");
});
Another issue might be that you have multiple elements with the same id.(另一个问题可能是您有多个具有相同ID的元素。) You should give them css classes like add
and then select for .add
.(您应该给他们css类,例如add
,然后为.add
选择。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…