javascript - 如何制作可以用 x 按钮关闭的广告横幅?
问题描述
这是我试过的,不起作用。我的网页上的结果是底部的空白 div。我希望当广告显示关闭时,CSS会使页面变暗。但事实并非如此。
function adTimer() {
setTimeout(banner, 5000);
}
var btn = document.querySelector('#btn-dismiss')
btn.addEventListener("click", function() {
bannerad = document.querySelector('#darken');
bannerad.style.display = 'none';
document.querySelector("html").classList.remove("darkenPage");
});
function banner() {
btn.innerhtml = "x";
bannerad.innerhtml = "Click here to go to our request page";
document.Banner.src = 'images/banner.jpg';
document.querySelector("html").classList.add("darkenPage");
}
var image = new image();
image.src = 'images/banner.jpg';
html.darken {
background-color: black;
}
html.darkenP body {
opacity: 0.5;
}
<div align="center">
<div id="banner-ad">
<img width="400px" height="400px" style="border-radius:40px; font-size:40px;" name="Banner" />
<button id="btndismiss"></button>
</div>
</div>
解决方案
我已经重新开始并得到了我想要的东西。这是我使用的 JavaScript:
var modal = document.getElementById("myModal");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
window.onload = function() {
setTimeout(function (){modal.style.display="block";},5000);
}
// 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";
}
}
这是我使用的html:
<div id="myModal" class="modal">
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>texxt</h2>
</div>
<div class="modal-body">
<p>text</p>
<p>text</p>
</div>
<div class="modal-footer">
<h3>footer text</h3>
</div>
</div>
</div>
推荐阅读
- oracle - FRM-40735:插入后触发器引发未处理的异常 ora-01722
- angular - 更新的组件字段在 UI 上没有改变
- java - 调用 serviceAccount.json 抛出 FileNotFoundException
- c++ - 如何为模板类的专用版本添加成员变量?
- ios - Apple 的 LLVM 中的优化错误,还是代码中的错误?
- node.js - 选择表数据时出现错误“参数类型错误、超出可接受范围或相互冲突”
- angular - 无法在 Ionic 4 中获取路由器中的参数发送
- node.js - 为什么 Postman 在我的 React 应用程序中接收到 express session cookie 但没有接收到我的 post 请求
- ffi - Nim 从 C 头文件导入 typedef
- sql - Redshift 中日期值之间的重复行