javascript-如何向我的html页面添加第二模式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-如何向我的html页面添加第二模式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8220字,纯文字阅读大概需要12分钟。
内容图文
![javascript-如何向我的html页面添加第二模式](/upload/InfoBanner/zyjiaocheng/691/75fc42406abc4bb99bd8989325c1d935.jpg)
我在上一篇文章中没有解释我的问题.
因此,我想在我的html页面中添加第二个模式,因此,如果单击“按钮1”,它将打开“模式1”,如果单击“按钮2”,它将打开“模式2”,但是会打开“模式3”. 4”“按钮5”和“按钮6”打开“模式2”.当我创建第二个模式并在下面设置JavaScript时.它将保持打开两个按钮上的模式2,而不是打开按钮1上的模式1.
另请注意,第一个按钮的ID为“免费”
另请注意,第二个按钮“ oneday”的ID
<script>
// Get the modal1
var modal = document.getElementById('myModal1');
// Get the button that opens the modal
var btn = document.getElementById("free");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on 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>
<script>
// Get the modal2
var modal = document.getElementById('myModal2');
// Get the button that opens the modal
var btn = document.getElementById("oneday");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on 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>
当按钮1设置为“ Modal1”时,两个按钮均保持打开状态“ Modal2”
下面的HTML
按钮1
<!-- service item -->
<div class="col-md-4 wow fadeInLeft" data-wow-duration="500ms">
<div class="service-item">
<div class="service-icon">
<i class="fa fa-snowflake-o fa-2x"></i>
</div>
<div class="service-desc">
<button id="free">
<h3>5 Day Free Trial</h3>
</button>
<b><p>So you can get a taste of the action for free, we will give new users a 5 day trial to see how they like our bot before they buy!</p> </b>
</div>
</div>
</div>
<!-- end service item -->
按钮2
<!-- service item -->
<div class="col-md-4 wow fadeInUp" data-wow-duration="500ms" data-wow-delay="500ms">
<div class="service-item">
<div class="service-icon">
<i class="fa fa-clock-o fa-2x"></i>
</div>
<div class="service-desc">
<button id="oneday">
<h3>1 Day</h3>
</button>
<b><p>$0.74 -24 Hours </p> </b>
</div>
</div>
</div>
<!-- end service item -->
模态在这里
Modal1
<!-- Modal1 -->
<div id="myModal1" class="modal">
<!-- Modal content -->
<div class="modal-content">
<form name="getinfo" onsubmit="return validateForm()" action="php/gmail.php" method="POST">
<div class="form-style-8">
<span class="close">×</span>
<label for="msg">E-mail:</label>
<input type="email" id="mail" name="email" />
</div>
<div>
<label for="msg">Username:</label>
<br>
<input id="user" name="username">
</textarea>
</div>
<div>
<label for="msg">Password:</label>
<br>
<input type="password" id="pass" name="password">
</textarea>
</div>
<div>
<label for="msg">Confirm Password:</label>
<br>
<input type="password" id="pass" name="cpassword">
</textarea>
</div>
<div>
<label for="msg">3 Hashtags:</label>
<br>
<input id="tags" name="hashtags">
</textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit">
</div>
</form>
</div>
</div>
Modal2
<!-- Modal2 content -->
<div class="modal-content">
<form name="getinfo2" onsubmit="return validateForm()" action="php/gmail.php" method="POST">
<div class="form-style-8">
<span class="close">×</span>
<label for="msg">Example:</label>
<input type="email" id="e1" name="email" />
</div>
<div>
<label for="msg">Example2:</label>
<br>
<input id="e2" name="username">
</textarea>
</div>
<div>
<label for="msg">Example3:</label>
<br>
<input type="password" id="e3" name="password">
</textarea>
</div>
<div>
<label for="msg">Example4:</label>
<br>
<input type="password" id="e4" name="cpassword">
</textarea>
</div>
<div>
<label for="msg">Example5:</label>
<br>
<input id="e5" name="hashtags">
</textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit">
</div>
</form>
</div>
</div>
所以我问我如何让modal1仅与button1和modal2一起与按钮2、3、4、5和6一起使用,我将在以后创建它们.
解决方法:
您只需要一个模态-它只是一个DIV结构,没有什么魔术(唯一的魔术就是使它出现/消失-引导程序可以做到这一点).必要时,从存储的位置获取所需的内容,将其粘贴到模态的.modal-body div中,并让引导程序将其显示.
这是如何做.
(1)将每个模式的内容存储在文档中其他位置的隐藏DIV中.
(2)给每个按钮一个ID,该ID具有可以隔离的唯一编号,并为每个“隐藏模式内容div”命名,并以相同的编号结尾.例如,按钮“#btn_2”将与隐藏的div“#mdl2”匹配,其中包含所需的按钮#btn_2的内容.
(3)使用javascript(您正在使用使用jQuery的Bootstrap,因此为什么不使用jQuery)来交换正确的内容.当您单击按钮时,隔离数字2(var buttNum = this.id.split(‘_’)[1];-尝试一下),然后使用它从#mdl2 div中抓取html(var content = $( ‘#mdl’buttNum).html())
(4)让Bootstrap负责打开/关闭模态.
例:
$('[id^=btn_]').click(function(){
var buttNum = this.id.split('_')[1];
//alert(buttNum);
var content = $('#mdl'+buttNum).html();
$('#myModal1 .modal-body').html(content);
});
#mdl1, #mdl2, #mdl3{display:none;} /* Hide the divs containing modal content */
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<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>
</head>
<body>
<div class="container">
<h3>Re-Using the Same Modal</h3>
<!-- Trigger the modal with a button -->
<button id="btn_1" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1">Open Modal One</button>
<button id="btn_2" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1">Open Modal Two</button>
<button id="btn_3" type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1">Ogden Nash Poem</button>
<!-- Re-Usable Modal -->
<div class="modal fade" id="myModal1" role="dialog">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-body">
</div>
</div>
</div>
</div>
</div>
<!-- Stick your modal content in hidden divs at bottom of the page -->
<div id="mdl1">
<div>Click outside modal to close modal</div>
<form name="getinfo" onsubmit="return validateForm()" action="php/gmail.php" method="POST">
<div class="form-style-8">
<span class="close">×</span>
<label for="msg">E-mail:</label>
<input type="email" id="mail" name="email" />
</div>
<div>
<label for="msg">Username:</label>
<br>
<input id="user" name="username">
</div>
<div>
<label for="msg">Password:</label>
<br>
<input type="password" id="pass" name="password">
</div>
<div>
<label for="msg">Confirm Password:</label>
<br>
<input type="password" id="pass" name="cpassword">
</div>
<div>
<label for="msg">3 Hashtags:</label>
<br>
<input id="tags" name="hashtags">
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit">
</div>
</form>
</div>
<div id="mdl2">
<div>Click outside modal to close modal</div>
<h1>Image of an Animal</h1>
<img src="http://placeimg.com/200/200/animals" />
</div><!-- #mdl2 -->
<div id="mdl3">
<div>Click outside modal to close modal</div>
<h1>Poem by Ogden Nash</h1>
<div style="font-size:1.4rem;">
<div>The hands of the clock were reaching high</div>
<div>In an old midtown hotel;</div>
<div>I name no name, but its sordid fame</div>
<div>Is table talk in hell.</div>
<div>I name no name, but hell's own flame</div>
<div>Illumes the lobby garish,</div>
<div>A gilded snare just off Times Square</div>
<div>For the maidens of the parish.</div>
<div>The revolving door swept the grimy floor</div>
<div>Like a crinoline grotesque,</div>
<div>And a lowly bum from an ancient slum</div>
<div>Crept furtively past the desk.</div>
<div>His footsteps sift into the lift</div>
<div>As a knife in the sheath is slipped,</div>
<div>Stealthy and swift into the lift</div>
<div>As a vampire into a crypt.</div>
</div>
</div><!-- #mdl3 -->
</body>
</html>
内容总结
以上是互联网集市为您收集整理的javascript-如何向我的html页面添加第二模式全部内容,希望文章能够帮你解决javascript-如何向我的html页面添加第二模式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。