javascript - 为什么不使用此代码打开此模式。它仍然重定向,但模式没有弹出
问题描述
我更新了 JS 以显示所有内容。下面的一个答案似乎不适用于我的所有代码。
我正在使用这个 JS
// 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 on the button, open the modal
btn = document.getElementById('myBtn');
modal = document.getElementById('myModal');
btn.onclick = function() {
modal.style.display = "block";
(function(){
var countdown = 5;
setInterval(function() {
countdown--;
if (countdown >= 0) {
span = document.getElementById("countdown");
span.innerHTML = countdown;
}
// Display 'counter' wherever you want to display it.
if (countdown === 0) {
// alert('this is where it happens');
window.location.href= 'https://smb.cyberpolicy.com';
clearInterval(countdown);
}
}, 1000);
})();
}
// 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";
}
}
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div id="boxes">
<div class="modal-content">
<div id="dialog" class="window">
<span class="close">×</span>
<p style="top-modal-text">In <span id="countdown">5</span> seconds you'll be leaving Hiscox to continue a quote with a trusted partner.</p>
<a href="http://smb.cyberpolicy.com/" class="no-external-link"><img alt="Cyber Security Get A Quote" src="https://www.hiscox.com/sites/default/files/images/wysiwyg/getaquote.jpg" style="margin-top: 10px" class="align-left"></a><br>
<a src="https://www.hiscox.com"><p>Click here to return to Hiscox.com</p></a>
</div>
</div>
<!-- <div id="mask"></div> -->
</div></div>
</div>
我认为 JS 有问题,但我显然错过了它。
解决方案
介绍
你有我修复的三个错误。
前两个是您从未为模式和按钮分配变量。您需要使用document.geteElementById
.
另一个错误是从一开始就没有隐藏模式。
在线小提琴:https ://jsfiddle.net/euo4byj2/31/
具体修复
您缺少变量分配:
btn = document.getElementById('myBtn');
modal = document.getElementById('myModal');
您还缺少首先隐藏模式的样式代码:
<div id="myModal" class="modal" style="display:none">
工作代码
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal" style="display:none">
<!-- Modal content -->
<div id="boxes">
<div class="modal-content">
<div id="dialog" class="window">
<span class="close">×</span>
<p style="top-modal-text">In <span id="countdown">5</span> seconds you'll be leaving Hiscox to continue a quote with a trusted partner.</p>
<a href="http://smb.cyberpolicy.com/" class="no-external-link"><img alt="Cyber Security Get A Quote" src="https://www.hiscox.com/sites/default/files/images/wysiwyg/getaquote.jpg" style="margin-top: 10px" class="align-left"></a><br>
<a src="https://www.hiscox.com"><p>Click here to return to Hiscox.com</p></a>
</div>
</div>
<!-- <div id="mask"></div> -->
</div></div>
</div>
<script>
btn = document.getElementById('myBtn');
modal = document.getElementById('myModal');
btn.onclick = function() {
modal.style.display = "block";
(function(){
var countdown = 5;
setInterval(function() {
countdown--;
if (countdown >= 0) {
span = document.getElementById("countdown");
span.innerHTML = countdown;
}
// Display 'counter' wherever you want to display it.
if (countdown === 0) {
// alert('this is where it happens');
window.location.href= 'https://smb.cyberpolicy.com';
clearInterval(countdown);
}
}, 1000);
})();
}
</script>
推荐阅读
- java - Java - 保存在 Linux 属性文件中时,单斜杠被转换为双斜杠
- node.js - 找不到模块正在寻找错误的目录
- ionic-framework - ionic 4 电容器不适用于 ionic/angular 我需要添加事件
- stm32 - STM32H7 FPU 中未设置除零异常标志
- node.js - 找不到源文件。在 Typescript 中导入 `.graphqls` 文件
- oracle-apex - 如何在交互式网格中使用 blob 数据类型
- cucumber - 黄瓜报告生成不起作用。请向我提供 jenkins 管道和 Cucumber runner 代码部分
- .htaccess - 如何在特定循环中添加 RewriteCond %{REQUEST_URI}
- javascript - 用 JS 函数附加 HTML 的正确语法是什么?
- python - 尝试拟合分类器 sklearn 时出现值错误