[js] Create an alert funtion by javascript
document.addEventListener('DOMContentLoaded', function() {
(function() {
// 创建模态对话框的 HTML 结构
var modalHTML = `
<div id="myModal" class="modal">
<div class="modal-content">
<p id="modal-message"></p>
<button id="confirm-button">确认</button>
</div>
</div>
`;
// 创建样式表
var styleHTML = `
<style>
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
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 {
background-color: #fefefe;
margin: 15% auto; /* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%; /* Could be more or less, depending on screen size */
}
</style>
`;
// 将 HTML 和 CSS 插入到页面中
document.head.insertAdjacentHTML('beforeend', styleHTML);
document.body.insertAdjacentHTML('beforeend', modalHTML);
// 获取模态对话框元素
var modal = document.getElementById('myModal');
var modalMessage = document.getElementById('modal-message');
var confirmButton = document.getElementById('confirm-button');
// 保存原始的 alert 函数
var originalAlert = window.alert;
// 重写 alert 函数
window.alert = function(message) {
// 设置消息
modalMessage.textContent = message;
// 显示模态对话框
modal.style.display = 'block';
// 当用户点击模态对话框外部区域时,隐藏模态对话框
window.onclick = function(event) {
if (event.target === modal) {
modal.style.display = 'none';
}
};
// 当用户点击确认按钮时,隐藏模态对话框
confirmButton.onclick = function() {
modal.style.display = 'none';
};
};
// 提供恢复原始 alert 函数的方法
window.restoreAlert = function() {
window.alert = originalAlert;
};
})();
}); 标签: JS
« 零到一
|
不要慌太阳下山有月光»
日历
最新微语
- 有的时候,会站在分叉路口,不知道向左还是右
2023-12-26 15:34
- 繁花乱开,鸟雀逐风。心自宁静,纷扰不闻。
2023-03-14 09:56
- 对于不可控的事,我们保持乐观,对于可控的事情,我们保持谨慎。
2023-02-09 11:03
- 小时候,
暑假意味着无忧无虑地玩很长一段时间,
节假意味着好吃好喝还有很多长期不见的小朋友来玩...
长大后,
这是女儿第一个暑假,
一个半月...
2022-07-11 08:54
- Watching the autumn leaves falling as you grow older together
2018-10-25 09:45
分类
最新评论
- Goonog
i get it now :) - 萧
@Fluzak:The web host... - Fluzak
Nice blog here! Also... - Albertarive
In my opinion you co... - ChesterHep
What does it plan? - ChesterHep
No, opposite. - mojoheadz
Everything is OK!... - Josephmaigh
I just want to say t... - ChesterHep
What good topic - AnthonyBub
Certainly, never it ...
发表评论: