Javascript 如何使用链接而不是按钮打开模式弹出窗口
如何使用链接而不是按钮弹出打开模式?我不希望该操作是一个按钮,我希望它是一个href链接,一个工作示例将非常感谢。下面提供了完整的代码。按钮,链接规则 现在,我把代码作为打开模式的按钮,按钮是预先设置好的样式,我只想使用主菜单的常规链接打开模式,正如你所看到的,我不想在主菜单中设置按钮,因为主菜单中有为菜单设置样式的链接Javascript 如何使用链接而不是按钮打开模式弹出窗口,javascript,hyperlink,modal-dialog,popup,Javascript,Hyperlink,Modal Dialog,Popup,如何使用链接而不是按钮弹出打开模式?我不希望该操作是一个按钮,我希望它是一个href链接,一个工作示例将非常感谢。下面提供了完整的代码。按钮,链接规则 现在,我把代码作为打开模式的按钮,按钮是预先设置好的样式,我只想使用主菜单的常规链接打开模式,正如你所看到的,我不想在主菜单中设置按钮,因为主菜单中有为菜单设置样式的链接 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial, Helvetica, sans-serif;}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<h2>Modal Example</h2>
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<p>Some text in the Modal..</p>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>```
正文{字体系列:Arial,Helvetica,无衬线;}
/*模态(背景)*/
.莫代尔{
显示:无;/*默认情况下隐藏*/
位置:固定;/*保持原位*/
z指数:1;/*位于顶部*/
填充顶部:100px;/*框的位置*/
左:0;
排名:0;
宽度:100%;/*全宽*/
高度:100%;/*全高*/
溢出:自动;/*根据需要启用滚动*/
背景色:rgb(0,0,0);/*回退色*/
背景色:rgba(0,0,0,0.4);/*黑色w/不透明度*/
}
/*模态内容*/
.模态内容{
背景色:#fefe;
保证金:自动;
填充:20px;
边框:1px实心#888;
宽度:80%;
}
/*关闭按钮*/
.结束{
颜色:#AAAAA;
浮动:对;
字号:28px;
字体大小:粗体;
}
.关闭:悬停,
.结束:聚焦{
颜色:#000;
文字装饰:无;
光标:指针;
}
模态示例
开放模态
&时代;
模态中的一些文本
//获取模态
var modal=document.getElementById(“myModal”);
//获取打开模式对话框的按钮
var btn=document.getElementById(“myBtn”);
//获取关闭模态的元素
var span=document.getElementsByClassName(“关闭”)[0];
//当用户单击该按钮时,打开模式对话框
btn.onclick=函数(){
modal.style.display=“块”;
}
//当用户单击(x)时,关闭模式对话框
span.onclick=函数(){
modal.style.display=“无”;
}
//当用户单击模式之外的任何位置时,将其关闭
window.onclick=函数(事件){
如果(event.target==模态){
modal.style.display=“无”;
}
}
```
要使用链接,您可以使用带有#
src的a
标记:
演示:
//获取模式
var modal=document.getElementById(“myModal”);
//获取打开模式对话框的按钮
var btn=document.getElementById(“myBtn”);
//获取关闭模态的元素
var span=document.getElementsByClassName(“关闭”)[0];
//当用户单击该按钮时,打开模式对话框
btn.onclick=函数(){
modal.style.display=“块”;
}
//当用户单击(x)时,关闭模式对话框
span.onclick=函数(){
modal.style.display=“无”;
}
//当用户单击模式之外的任何位置时,将其关闭
window.onclick=函数(事件){
如果(event.target==模态){
modal.style.display=“无”;
}
}
body{font-family:Arial、Helvetica、sans-serif;}
/*模态(背景)*/
.莫代尔{
显示:无;/*默认情况下隐藏*/
位置:固定;/*保持原位*/
z指数:1;/*位于顶部*/
填充顶部:100px;/*框的位置*/
左:0;
排名:0;
宽度:100%;/*全宽*/
高度:100%;/*全高*/
溢出:自动;/*根据需要启用滚动*/
背景色:rgb(0,0,0);/*回退色*/
背景色:rgba(0,0,0,0.4);/*黑色w/不透明度*/
}
/*模态内容*/
.模态内容{
背景色:#fefe;
保证金:自动;
填充:20px;
边框:1px实心#888;
宽度:80%;
}
/*关闭按钮*/
.结束{
颜色:#AAAAA;
浮动:对;
字号:28px;
字体大小:粗体;
}
.关闭:悬停,
.结束:聚焦{
颜色:#000;
文字装饰:无;
光标:指针;
}
模态示例
&时代;
模态中的一些文本
只需更改
的按钮,当前脚本将分配onclick方法。