<link rel="stylesheet" href="hiAlert.css">
<script src="jquery.min.js"></script>
<script src="jquery.hiAlert.js"></script>
<script>
hiAlert('歡迎來到 helloweba.com', '提示');
hiConfirm('確認刪除?', '確認', r => { if (r) console.log('已刪除'); });
hiPrompt('請輸入名字', '預設值', '輸入', r => { if (r) console.log(r); });
hiBox('#hiddenDiv', '標題', 400, 300, '', '.close-btn');
hiOverAlert('操作成功!', 2000); // 頂部漸變提示條
</script>
| 方法 | 參數 | 說明 |
|---|---|---|
| hiAlert | content | 提示內容 (HTML 字串) |
| title | 標題 (可選) | |
| callback | 關閉後回調 `function()` (可選) | |
| hiConfirm | content | 提示內容 |
| title | 標題 | |
| callback | `function(confirmed: boolean)` 點確定/取消 | |
| hiPrompt | content | 提示內容 |
| defaultVal | 輸入框預設值 | |
| title | 標題 | |
| callback | `function(value: string|null)` 取消返回 null | |
| hiBox | obj | 選擇器/元素:彈窗內容容器 (需隱藏在頁面) |
| title | 標題 | |
| w | 寬度 (px) | |
| h | 高度 (px/'' 自適應) | |
| submit | 提交按鈕選擇器 (可選) | |
| cancel | 取消/關閉按鈕選擇器 (可選) | |
| callback | `function(data)` 提交時回調 | |
| hiOverAlert | content | 提示條內容 |
| timeout | 自動關閉毫秒數 (預設 3000) |
<!-- HTML -->
<dialog id="alertDlg">
<form method="dialog">
<p id="alertMsg"></p>
<menu><button value="ok" autofocus>確定</button></menu>
</form>
</dialog>
<dialog id="confirmDlg">
<form method="dialog">
<p id="confirmMsg"></p>
<menu>
<button value="cancel">取消</button>
<button value="ok" id="confirmOk" autofocus>確定</button>
</menu>
</form>
</dialog>
<style>
dialog { border: none; border-radius: 8px; box-shadow: 0 12px 32px rgba(0,0,0,.2); padding: 0; max-width: 90vw; }
dialog::backdrop { background: rgba(0,0,0,.4); backdrop-filter: blur(2px); }
dialog[open] { animation: fadeIn .2s ease; }
@keyframes fadeIn { from { opacity: 0; transform: scale(.95); } to { opacity: 1; transform: scale(1); } }
dialog form { padding: 24px; display: flex; flex-direction: column; gap: 16px; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; margin: 0; padding: 0 24px 24px; }
dialog button { padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; }
button[value="ok"] { background: #1976d2; color: #fff; }
button[value="cancel"] { background: #e0e0e0; }
</style>
<script>
// Alert
function alert(message, title = '提示') {
const dlg = document.getElementById('alertDlg');
dlg.querySelector('#alertMsg').textContent = message;
return dlg.showModal(); // 返回 Promise,關閉後 resolve(returnValue)
}
// Confirm
function confirm(message, title = '確認') {
const dlg = document.getElementById('confirmDlg');
dlg.querySelector('#confirmMsg').textContent = message;
return dlg.showModal().then(v => v === 'ok');
}
// Prompt
function prompt(message, defaultVal = '', title = '輸入') {
// 需自建含 input 的 dialog,略
}
// Toast / OverAlert (頂部提示條)
function toast(message, ms = 3000) {
const el = document.createElement('div');
el.textContent = message;
el.style.cssText = 'position:fixed;top:20px;left:50%;transform:translateX(-50%);background:#333;color:#fff;padding:12px 24px;border-radius:8px;z-index:9999;animation:slideDown .3s ease';
document.body.appendChild(el);
setTimeout(() => { el.style.animation = 'slideUp .3s ease forwards'; el.addEventListener('animationend',()=>el.remove()); }, ms);
}
// 使用
alert('歡迎!').then(() => console.log('已關閉'));
confirm('確定刪除?').then(ok => ok && console.log('刪除'));
toast('保存成功', 2000);
</script>
<button popovertarget="myPopover">打開</button>
<div id="myPopover" popover>
內容區域
<button popovertarget="myPopover" popovertargetaction="hide">關閉</button>
</div>
<style>
[popover] { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); border: none; border-radius: 8px; box-shadow: 0 12px 32px rgba(0,0,0,.2); padding: 24px; background: white; }
[popover]::backdrop { background: rgba(0,0,0,.4); }
</style>
零 JS、瀏覽器原生管理焦點陷阱、ESC 關閉、背景遮罩、動畫 (配合 `@starting-style`)。
| 方案 | 體積 | 依賴 | 瀏覽器支援 | 功能完整度 | 推薦場景 |
|---|---|---|---|---|---|
| hiAlert | 8 KB | jQuery + jQuery UI (拖曳) | IE6+ | 完整 (5 方法) | 老專案、需 IE 支援、快速接入 |
| ` | ~1 KB | 無 | Chrome 37+, FF 53+, Safari 15.4+ | 完整 (焦點陷阱、模態、表單關閉) | **現代專案首選** |
| Popover API | 0 KB (純 HTML/CSS) | 無 | Chrome 114+, Edge 114+, Safari 17+ | 非模態、輕量提示、菜單、Tooltip | 非模態浮層、下拉選單 |
| SweetAlert2 | 40 KB | 無 (可選 jQuery) | 現代瀏覽器 | 極豐富 (圖標、動畫、隊列、主題) | 需華麗視覺、複雜交互 |
hiAlert 在 jQuery 時代是優秀的輕量彈窗方案。現代專案**強烈建議遷移到原生 `