You're using JQuery
remove
, which effectively deletes the alert from your DOM.(您正在使用JQuery
remove
,它可以有效地从DOM中删除警报。)
So showing it again without reloading your page is not possible.(因此,无法重新显示而不重新加载页面是不可能的。)
But you could use JQuery
detach
instead.(但是您可以改用JQuery
detach
。) Then you can insert it again with appendTo
.(然后,您可以使用appendTo
再次将其插入。)
var alert = null; function showHideAlert() { if (alert) { alert.appendTo("body"); alert = null; } else { alert = $(".alert").detach(); } }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <body> <button onclick="showHideAlert();">Show/Hide alert</button> <div class="alert">ALERT!!!</div> </body>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…