卡片標題
內容摘要...
閱讀全文 →
$('.clickable-box').on('click', function(e) {
// 忽略內部可互動元素(連結、按鈕、輸入框)的點擊
if ($(e.target).is('a, button, input, select, textarea, label')) return;
const link = $(this).find('a[href]').first();
if (link.length) {
window.location.href = link.attr('href');
}
});
卡片標題
內容摘要...
閱讀全文 →
卡片標題
內容摘要...
卡片標題
內容摘要...
.clickable-box {
cursor: pointer;
transition: box-shadow 0.2s, transform 0.1s;
}
.clickable-box:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.15); transform: translateY(-2px); }
.clickable-box:active { transform: translateY(0); }
.clickable-box:focus-visible { outline: 2px solid #0066cc; outline-offset: 4px; }
const vClickable = {
mounted(el, binding) {
el.style.cursor = 'pointer';
el.tabIndex = 0;
el.setAttribute('role', 'button');
const handler = (e) => {
if (e.type === 'keydown' && !['Enter', ' '].includes(e.key)) return;
e.preventDefault();
window.location.href = binding.value;
};
el.addEventListener('click', handler);
el.addEventListener('keydown', handler);
el._clickableHandler = handler;
},
unmounted(el) {
el.removeEventListener('click', el._clickableHandler);
el.removeEventListener('keydown', el._clickableHandler);
}
};
app.directive('clickable', vClickable);
// 使用
...
function ClickableCard({href, children, ...props}) {
const handleKeyDown = (e) => {
if (['Enter', ' '].includes(e.key)) {
e.preventDefault();
window.location.href = href;
}
};
return (
window.location.href = href}
onKeyDown={handleKeyDown}
tabIndex={0}
role="button"
aria-label={props['aria-label']}
style={{cursor: 'pointer'}}
>
{children}
);
}
| 方案 | 語意性 | 無障礙 | SEO | 巢狀互動元素 | 推薦 |
|---|---|---|---|---|---|
| DIV + JS | 低 | 需大量 ARIA | 差 | 支援 | ⭐ |
| 外層 A 包裹 | 高 | 原生完美 | 優 | 不支援 | ⭐⭐⭐⭐⭐ |
| Button + JS | 中 | 良好 | 中 | 支援 | ⭐⭐⭐⭐ |
原則:能用連結就用連結,能用按鈕就用按鈕。只有在極罕見情況(如複雜巢狀互動區塊整體跳轉)才考慮 DIV + JS 方案,並務必完整實作無障礙屬性。