$('#el').live('click', function(e) {
if ((!$.browser.msie && e.button === 0) || ($.browser.msie && e.button === 1)) {
// 左鍵
} else if (e.button === 2) {
// 右鍵
}
});
問題:
.live() 已在 jQuery 1.7 棄用、1.9 移除$.browser 已在 jQuery 1.9 移除click 事件僅觸發於左鍵,右鍵不會觸發 click| 事件 | 觸發按鍵 | e.button 值 | e.which 值 |
|---|---|---|---|
mousedown / mouseup | 左/中/右 | 0/1/2 | 1/2/3 |
click | 僅左鍵 | 0 | 1 |
contextmenu | 右鍵 | 2 | 3 |
auxclick | 非左鍵(中/右) | 1/2 | 2/3 |
// 1. 檢測按下/彈起時的具體按鍵
$('#target').on('mousedown', function(e) {
// e.which: 1=左, 2=中, 3=右(標準化,跨瀏覽器一致)
switch (e.which) {
case 1: console.log('左鍵按下'); break;
case 2: console.log('中鍵按下'); break;
3: console.log('右鍵按下'); break;
}
});
// 2. 僅攔截右鍵菜單(常見需求)
$('#target').on('contextmenu', function(e) {
e.preventDefault(); // 阻止默認右鍵菜單
console.log('右鍵點擊,座標:', e.clientX, e.clientY);
// 顯示自定義菜單
showCustomMenu(e.clientX, e.clientY);
});
// 3. 輔助按鍵(中鍵、側鍵)用 auxclick
$('#target').on('auxclick', function(e) {
if (e.button === 1) console.log('中鍵點擊');
if (e.button === 3) console.log('側鍵/右鍵點擊(非標準contextmenu)');
});
const el = document.getElementById('target');
el.addEventListener('mousedown', e => {
const btn = ['左鍵', '中鍵', '右鍵', '側鍵1', '側鍵2'][e.button] || `按鍵${e.button}`;
console.log(`${btn} 按下 at (${e.clientX}, ${e.clientY})`);
});
el.addEventListener('contextmenu', e => {
e.preventDefault();
showCustomMenu(e.clientX, e.clientY);
});
// 統一處理鼠標、觸控、觸控筆
el.addEventListener('pointerdown', e => {
// e.button: 0=主按鍵(左/觸控), 1=輔助(中), 2=次要(右), 3=X1(後退), 4=X2(前進)
// e.pointerType: 'mouse' | 'touch' | 'pen'
if (e.pointerType === 'mouse') {
switch (e.button) {
case 0: handleLeftClick(e); break;
case 2: handleRightClick(e); break;
}
} else if (e.pointerType === 'touch') {
handleTouchStart(e);
}
});
// CSS 防止觸發默認觸控行為
// #target { touch-action: none; }
<!DOCTYPE html>
<html><head><meta charset="utf-8"><style>
#target { width:300px; height:200px; background:#f0f0f0; border:1px solid #ccc; display:flex; align-items:center; justify-content:center; user-select:none; }
</style></head><body>
<div id="target">在此區域點擊鼠標左/中/右鍵</div>
<pre id="log"></pre>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
<script>
const $log = $('#log');
function log(msg) { $log.text(msg + '\n' + $log.text().slice(0, 500)); }
$('#target').on('mousedown', e => log(`mousedown: which=${e.which}, button=${e.button}`));
$('#target').on('mouseup', e => log(`mouseup: which=${e.which}, button=${e.button}`));
$('#target').on('click', e => log(`click: which=${e.which}, button=${e.button}`));
$('#target').on('contextmenu', e => { e.preventDefault(); log(`contextmenu: 阻止默認菜單`); });
$('#target').on('auxclick', e => log(`auxclick: button=${e.button}`));
// 原生對比
document.getElementById('target').addEventListener('pointerdown', e => {
log(`pointerdown: type=${e.pointerType}, button=${e.button}, isPrimary=${e.isPrimary}`);
});
</script>
</body></html>
mousedown + e.which (1/3) 或 e.button (0/2)contextmenu,e.preventDefault()Pointer Events 統一鼠標/觸控/筆輸入.live()、.delegate()、$.browser、在 click 事件中判斷右鍵