1 回答

TA贡献1852条经验 获得超7个赞
在第二次单击中,您需要从正文委托事件。由于附加事件时该元素不存在于 DOM 中。所以将其更改$(".btn").click(function(){ 为 $("body").on('click', '.btn', function() {
$(function() {
var content1 = '<p>This is a paragraph</p><button class="btn">Show other content</button>';
var content2 = '<h1>This is a title</h1>';
$(".button").click(function() {
$(".modal").show();
$(".modal-content").html(content1);
});
$("body").on('click', '.btn', function() {
$(".modal-content").html(content2);
console.log(content2);
});
});
.modal {
width: 300px;
hieght: 300px;
margin: auto;
display: none;
background: #eee;
padding: 20px 0;
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="button">Open modal</button>
<div class="modal">
<div class="modal-content"></div>
</div>
添加回答
举报