gpt4 book ai didi

javascript - 单击按钮后显示警报

转载 作者:行者123 更新时间:2023-11-30 15:37:23 25 4
gpt4 key购买 nike

我有一个按钮,在单击后会弹出一个警告“你好”。第一次点击它工作正常,但在我关闭警报并再次单击按钮后,警报不显示。请帮忙。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.hidden {
display: none;
}
</style>
<title>Show Alert</title>
</head>

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
<div class="container">

<div class="row">
<div class="alert alert-success hidden" id="success-alert">
<button type="button" class="close" data-dismiss="alert">x</button>
<span class="glyphicon glyphicon-ok"></span> Hello
</div>
</div>

<div class="row">
<button id="btn" type="button" class="btn btn-default">Open
Alert</button>
</div>
</div>

<script type="text/javascript">
$(document).ready(function() {
$("#btn").click(function() {
$('#success-alert').removeClass('hidden');
});

});
</script>
</body>

</html>

最佳答案

您的隐藏 类应该返回,但不是由于data-dismiss。从您的关闭按钮中删除 data-dismiss 并为其附加一个点击事件,这将在点击时返回 hidden 类。这是工作代码。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.hidden {
display: none;
}
</style>
<title>Show Alert</title>
</head>

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
<div class="container">

<div class="row">
<div class="alert alert-success hidden" id="success-alert">
<button type="button" class="close">x</button>
<span class="glyphicon glyphicon-ok"></span> Hello
</div>
</div>

<div class="row">
<button id="btn" type="button" class="btn btn-default">Open
Alert</button>
</div>
</div>

<script type="text/javascript">
$(document).ready(function() {
$("#btn").click(function() {
$('#success-alert').removeClass('hidden');
});
$("button.close").click(function() {
$('#success-alert').addClass('hidden');
});

});
</script>
</body>

</html>

关于javascript - 单击按钮后显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41319553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com