html - 单击html中的按钮时如何使弹出窗口出现
问题描述
我需要这方面的帮助,因为我一直在研究互联网,但没有发现任何好处,所以我试图在用户单击按钮时弹出一个窗口。这是我找到但不起作用的代码:
$('#show').on('click', function () {
$('.center').show();
$(this).hide();
})
$('#close').on('click', function () {
$('.center').hide();
$('#show').show();
})
.center {
margin: auto;
width: 60%;
padding: 20px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.hideform {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="center hideform">
<button id="close" style="float: right;">X</button>
<form action="/action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
</div>
<button id="show">Show form</button>
而且由于stackoverflow允许您测试代码,所以我做了并且它工作了,但是当我在我的网站上测试它时它没有工作。这是网站在此处输入链接描述。帮助表示赞赏!
解决方案
JavaScript 代码应该用<script>
标签包裹起来。CSS 代码应该用<style>
标签内的<head>
标签包裹起来(虽然不是强制性的)。
您应该使用的 HTML 是这样的:
<html>
<head>
<style>
.center {
margin: auto;
width: 60%;
padding: 20px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.hideform {
display: none;
}
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="center hideform">
<button id="close" style="float: right;">X</button>
<form action="/action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
</div>
<button id="show">Show form</button>
<script>
$('#show').on('click', function () {
$('.center').show();
$(this).hide();
})
$('#close').on('click', function () {
$('.center').hide();
$('#show').show();
})
</script>
</body>
</html>
推荐阅读
- c++ - C++ 草稿中的词包是什么?
- javascript - React - 如何使只有一个增量函数 setState 多个变量?
- svg - 在嵌套 SVG 中,内部图标被截断
- c - 使用 fseek 在 C 中重写文件
- spring - 如何有选择地使一个类成为应该在 Spring (Spring Boot 2.x) 中运行的代码
- c++ - C ++将引用传递给同一类中的函数
- node.js - ExpressJS 中间件方法使变量在其他(模块)文件中可用
- c++ - C++如何创建一个不允许模板参数的类模板
- java - Random.nextInt() 返回相同的数字
- windows - 为什么顶级窗口的 GetParent(hwnd) 和 (HWND)::GetWindow(hwnd, GW_OWNER) 会给出不同的结果?